12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353 |
- // SPDX-License-Identifier: GPL-2.0
- /*
- * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
- */
- #include <linux/init.h>
- #include <linux/module.h>
- #include <linux/device.h>
- #include <linux/slab.h>
- #include <linux/vmalloc.h>
- #include <linux/kthread.h>
- #include <linux/platform_device.h>
- #include <linux/of.h>
- #include <linux/delay.h>
- #include <linux/sched.h>
- #include <linux/freezer.h>
- #include <sound/soc.h>
- #include <sound/lsm_params.h>
- #include <sound/pcm_params.h>
- #include "msm-slim-dma.h"
- #include "codecs/cpe_core.h"
- #define DRV_NAME "msm-cpe-lsm"
- #define SAMPLE_RATE_48KHZ 48000
- #define SAMPLE_RATE_16KHZ 16000
- #define LSM_VOICE_WAKEUP_APP_V2 2
- #define AFE_PORT_ID_1 1
- #define AFE_PORT_ID_3 3
- #define AFE_OUT_PORT_2 2
- #define LISTEN_MIN_NUM_PERIODS 2
- #define LISTEN_MAX_NUM_PERIODS 12
- #define LISTEN_MAX_PERIOD_SIZE 61440
- #define LISTEN_MIN_PERIOD_SIZE 320
- #define LISTEN_MAX_STATUS_PAYLOAD_SIZE 256
- #define MSM_CPE_MAX_CUSTOM_PARAM_SIZE 2048
- #define MSM_CPE_LAB_THREAD_TIMEOUT (3 * (HZ/10))
- #define MSM_CPE_LSM_GRAB_LOCK(lock, name) \
- { \
- pr_debug("%s: %s lock acquire\n", \
- __func__, name); \
- mutex_lock(lock); \
- }
- #define MSM_CPE_LSM_REL_LOCK(lock, name) \
- { \
- pr_debug("%s: %s lock release\n", \
- __func__, name); \
- mutex_unlock(lock); \
- }
- /* Conventional and unconventional sample rate supported */
- static unsigned int supported_sample_rates[] = {
- 8000, 16000, 48000, 192000, 384000
- };
- static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
- .count = ARRAY_SIZE(supported_sample_rates),
- .list = supported_sample_rates,
- .mask = 0,
- };
- static struct snd_pcm_hardware msm_pcm_hardware_listen = {
- .info = (SNDRV_PCM_INFO_BLOCK_TRANSFER |
- SNDRV_PCM_INFO_MMAP_VALID |
- SNDRV_PCM_INFO_INTERLEAVED |
- SNDRV_PCM_INFO_PAUSE |
- SNDRV_PCM_INFO_RESUME),
- .formats = (SNDRV_PCM_FMTBIT_S16_LE |
- SNDRV_PCM_FMTBIT_S24_LE |
- SNDRV_PCM_FMTBIT_S32_LE),
- .rates = (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000 |
- SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_384000),
- .rate_min = 16000,
- .rate_max = 384000,
- .channels_min = 1,
- .channels_max = 1,
- .buffer_bytes_max = LISTEN_MAX_NUM_PERIODS *
- LISTEN_MAX_PERIOD_SIZE,
- .period_bytes_min = LISTEN_MIN_PERIOD_SIZE,
- .period_bytes_max = LISTEN_MAX_PERIOD_SIZE,
- .periods_min = LISTEN_MIN_NUM_PERIODS,
- .periods_max = LISTEN_MAX_NUM_PERIODS,
- .fifo_size = 0,
- };
- enum {
- AFE_CMD_INVALID = 0,
- AFE_CMD_PORT_START,
- AFE_CMD_PORT_SUSPEND,
- AFE_CMD_PORT_RESUME,
- AFE_CMD_PORT_STOP,
- };
- enum cpe_lab_thread_status {
- MSM_LSM_LAB_THREAD_STOP,
- MSM_LSM_LAB_THREAD_RUNNING,
- MSM_LSM_LAB_THREAD_ERROR,
- };
- struct cpe_hw_params {
- u32 sample_rate;
- u16 sample_size;
- u32 buf_sz;
- u32 period_count;
- u16 channels;
- };
- struct cpe_data_pcm_buf {
- u8 *mem;
- phys_addr_t phys;
- };
- struct cpe_lsm_lab {
- atomic_t in_count;
- atomic_t abort_read;
- u32 dma_write;
- u32 buf_idx;
- u32 pcm_size;
- enum cpe_lab_thread_status thread_status;
- struct cpe_data_pcm_buf *pcm_buf;
- wait_queue_head_t period_wait;
- struct completion comp;
- struct completion thread_complete;
- };
- struct cpe_priv {
- void *core_handle;
- struct snd_soc_codec *codec;
- struct wcd_cpe_lsm_ops lsm_ops;
- struct wcd_cpe_afe_ops afe_ops;
- bool afe_mad_ctl;
- u32 input_port_id;
- };
- struct cpe_lsm_data {
- struct device *dev;
- struct cpe_lsm_session *lsm_session;
- struct mutex lsm_api_lock;
- struct cpe_lsm_lab lab;
- struct cpe_hw_params hw_params;
- struct snd_pcm_substream *substream;
- wait_queue_head_t event_wait;
- atomic_t event_avail;
- atomic_t event_stop;
- u8 ev_det_status;
- u8 ev_det_pld_size;
- u8 *ev_det_payload;
- bool cpe_prepared;
- };
- static int msm_cpe_afe_mad_ctl_get(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- struct cpe_priv *cpe = kcontrol->private_data;
- ucontrol->value.integer.value[0] = cpe->afe_mad_ctl;
- return 0;
- }
- static int msm_cpe_afe_mad_ctl_put(struct snd_kcontrol *kcontrol,
- struct snd_ctl_elem_value *ucontrol)
- {
- struct cpe_priv *cpe = kcontrol->private_data;
- cpe->afe_mad_ctl = ucontrol->value.integer.value[0];
- return 0;
- }
- static struct snd_kcontrol_new msm_cpe_kcontrols[] = {
- SOC_SINGLE_EXT("CPE AFE MAD Enable", SND_SOC_NOPM, 0, 1, 0,
- msm_cpe_afe_mad_ctl_get, msm_cpe_afe_mad_ctl_put),
- };
- /*
- * cpe_get_private_data: obtain ASoC platform driver private data
- * @substream: ASoC substream for which private data to be obtained
- */
- static struct cpe_priv *cpe_get_private_data(
- struct snd_pcm_substream *substream)
- {
- struct snd_soc_pcm_runtime *rtd;
- if (!substream || !substream->private_data) {
- pr_err("%s: %s is invalid\n",
- __func__,
- (!substream) ? "substream" : "private_data");
- goto err_ret;
- }
- rtd = substream->private_data;
- if (!rtd || !rtd->platform) {
- pr_err("%s: %s is invalid\n",
- __func__,
- (!rtd) ? "runtime" : "platform");
- goto err_ret;
- }
- return snd_soc_platform_get_drvdata(rtd->platform);
- err_ret:
- return NULL;
- }
- /*
- * cpe_get_lsm_data: obtain the lsm session data given the substream
- * @substream: ASoC substream for which lsm session data to be obtained
- */
- static struct cpe_lsm_data *cpe_get_lsm_data(
- struct snd_pcm_substream *substream)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- return runtime->private_data;
- }
- static void msm_cpe_process_event_status(void *data,
- u8 detect_status, u8 size, u8 *payload)
- {
- struct cpe_lsm_data *lsm_d = data;
- lsm_d->ev_det_status = detect_status;
- lsm_d->ev_det_pld_size = size;
- lsm_d->ev_det_payload = kzalloc(size, GFP_KERNEL);
- if (!lsm_d->ev_det_payload)
- return;
- memcpy(lsm_d->ev_det_payload, payload, size);
- atomic_set(&lsm_d->event_avail, 1);
- wake_up(&lsm_d->event_wait);
- }
- static void msm_cpe_process_event_status_done(struct cpe_lsm_data *lsm_data)
- {
- kfree(lsm_data->ev_det_payload);
- lsm_data->ev_det_payload = NULL;
- lsm_data->ev_det_status = 0;
- lsm_data->ev_det_pld_size = 0;
- }
- /*
- * msm_cpe_afe_port_cntl: Perform the afe port control
- * @substream: substream for which afe port command to be performed
- * @core_handle: handle to core
- * @afe_ops: handle to the afe operations
- * @afe_cfg: afe port configuration data
- * @cmd: command to be sent to AFE
- *
- */
- static int msm_cpe_afe_port_cntl(
- struct snd_pcm_substream *substream,
- void *core_handle,
- struct wcd_cpe_afe_ops *afe_ops,
- struct wcd_cpe_afe_port_cfg *afe_cfg,
- int cmd)
- {
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- int rc = 0;
- if (!afe_cfg->port_id) {
- /*
- * It is possible driver can get closed without prepare,
- * in which case afe ports will not be initialized.
- */
- dev_dbg(rtd->dev,
- "%s: Invalid afe port id\n",
- __func__);
- return 0;
- }
- switch (cmd) {
- case AFE_CMD_PORT_START:
- rc = afe_ops->afe_port_start(core_handle, afe_cfg);
- if (rc != 0)
- dev_err(rtd->dev,
- "%s: AFE port start failed\n",
- __func__);
- break;
- case AFE_CMD_PORT_SUSPEND:
- rc = afe_ops->afe_port_suspend(core_handle, afe_cfg);
- if (rc != 0)
- dev_err(rtd->dev,
- "%s: afe_suspend failed, err = %d\n",
- __func__, rc);
- break;
- case AFE_CMD_PORT_RESUME:
- rc = afe_ops->afe_port_resume(core_handle, afe_cfg);
- if (rc != 0)
- dev_err(rtd->dev,
- "%s: afe_resume failed, err = %d\n",
- __func__, rc);
- break;
- case AFE_CMD_PORT_STOP:
- rc = afe_ops->afe_port_stop(core_handle, afe_cfg);
- if (rc != 0)
- dev_err(rtd->dev,
- "%s: afe_stopfailed, err = %d\n",
- __func__, rc);
- break;
- }
- return rc;
- }
- static int msm_cpe_lsm_lab_stop(struct snd_pcm_substream *substream)
- {
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct cpe_priv *cpe = cpe_get_private_data(substream);
- struct wcd_cpe_lsm_ops *lsm_ops;
- struct wcd_cpe_afe_ops *afe_ops;
- struct cpe_lsm_session *session;
- struct cpe_lsm_lab *lab_d = &lsm_d->lab;
- struct msm_slim_dma_data *dma_data = NULL;
- int rc;
- /*
- * the caller is not aware of LAB status and will
- * try to stop lab even if it is already stopped.
- * return success right away is LAB is already stopped
- */
- if (lab_d->thread_status == MSM_LSM_LAB_THREAD_STOP) {
- dev_dbg(rtd->dev,
- "%s: lab already stopped\n",
- __func__);
- return 0;
- }
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- if (!lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- __func__);
- return -EINVAL;
- }
- lsm_ops = &cpe->lsm_ops;
- afe_ops = &cpe->afe_ops;
- session = lsm_d->lsm_session;
- if (rtd->cpu_dai)
- dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
- substream);
- if (!dma_data || !dma_data->dai_channel_ctl) {
- dev_err(rtd->dev,
- "%s: dma_data is not set\n",
- __func__);
- return -EINVAL;
- }
- if (lab_d->thread_status == MSM_LSM_LAB_THREAD_RUNNING) {
- dev_dbg(rtd->dev, "%s: stopping lab thread\n",
- __func__);
- rc = kthread_stop(session->lsm_lab_thread);
- /*
- * kthread_stop returns EINTR if the thread_fn
- * was not scheduled before calling kthread_stop.
- * In this case, we dont need to wait for lab
- * thread to complete as lab thread will not be
- * scheduled at all.
- */
- if (rc == -EINTR)
- goto done;
- /* Wait for the lab thread to exit */
- rc = wait_for_completion_timeout(
- &lab_d->thread_complete,
- MSM_CPE_LAB_THREAD_TIMEOUT);
- if (!rc) {
- dev_err(rtd->dev,
- "%s: Wait for lab thread timedout\n",
- __func__);
- return -ETIMEDOUT;
- }
- }
- rc = lsm_ops->lab_ch_setup(cpe->core_handle,
- session,
- WCD_CPE_PRE_DISABLE);
- if (rc)
- dev_err(rtd->dev,
- "%s: PRE ch teardown failed, err = %d\n",
- __func__, rc);
- /* continue with teardown even if any intermediate step fails */
- rc = dma_data->dai_channel_ctl(dma_data, rtd->cpu_dai, false);
- if (rc)
- dev_err(rtd->dev,
- "%s: open data failed %d\n", __func__, rc);
- dma_data->ph = 0;
- /*
- * Even though LAB stop failed,
- * output AFE port needs to be stopped
- */
- rc = afe_ops->afe_port_stop(cpe->core_handle,
- &session->afe_out_port_cfg);
- if (rc)
- dev_err(rtd->dev,
- "%s: AFE out port stop failed, err = %d\n",
- __func__, rc);
- rc = lsm_ops->lab_ch_setup(cpe->core_handle,
- session,
- WCD_CPE_POST_DISABLE);
- if (rc)
- dev_err(rtd->dev,
- "%s: POST ch teardown failed, err = %d\n",
- __func__, rc);
- done:
- lab_d->thread_status = MSM_LSM_LAB_THREAD_STOP;
- lab_d->buf_idx = 0;
- atomic_set(&lab_d->in_count, 0);
- lab_d->dma_write = 0;
- return 0;
- }
- static int msm_cpe_lab_buf_alloc(struct snd_pcm_substream *substream,
- struct cpe_lsm_session *session,
- struct msm_slim_dma_data *dma_data)
- {
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct cpe_lsm_lab *lab_d = &lsm_d->lab;
- struct cpe_hw_params *hw_params = &lsm_d->hw_params;
- struct cpe_data_pcm_buf *pcm_buf = NULL;
- int rc = 0;
- int dma_alloc = 0;
- u32 count = 0;
- u32 bufsz, bufcnt;
- if (lab_d->pcm_buf &&
- lab_d->pcm_buf->mem) {
- dev_dbg(rtd->dev,
- "%s: LAB buf already allocated\n",
- __func__);
- goto exit;
- }
- bufsz = hw_params->buf_sz;
- bufcnt = hw_params->period_count;
- dev_dbg(rtd->dev,
- "%s:Buf Size %d Buf count %d\n",
- __func__,
- bufsz, bufcnt);
- pcm_buf = kzalloc(((sizeof(struct cpe_data_pcm_buf)) * bufcnt),
- GFP_KERNEL);
- if (!pcm_buf) {
- rc = -ENOMEM;
- goto exit;
- }
- lab_d->pcm_buf = pcm_buf;
- dma_alloc = bufsz * bufcnt;
- pcm_buf->mem = NULL;
- pcm_buf->mem = kzalloc(dma_alloc, GFP_DMA);
- if (!pcm_buf->mem) {
- rc = -ENOMEM;
- goto fail;
- }
- pcm_buf->phys = dma_map_single(dma_data->sdev->dev.parent,
- pcm_buf->mem, dma_alloc, DMA_BIDIRECTIONAL);
- if (dma_mapping_error(dma_data->sdev->dev.parent, pcm_buf->phys)) {
- dev_err(rtd->dev, "%s Error mapping DMA buffers\n", __func__);
- pcm_buf->phys = (phys_addr_t)NULL;
- rc = -EFAULT;
- goto fail;
- }
- count = 0;
- while (count < bufcnt) {
- pcm_buf[count].mem = pcm_buf[0].mem + (count * bufsz);
- pcm_buf[count].phys = pcm_buf[0].phys + (count * bufsz);
- dev_dbg(rtd->dev,
- "%s: pcm_buf[%d].mem %pK pcm_buf[%d].phys %pK\n",
- __func__, count,
- (void *)pcm_buf[count].mem,
- count, &(pcm_buf[count].phys));
- count++;
- }
- return 0;
- fail:
- if (pcm_buf && pcm_buf->mem)
- kfree(pcm_buf->mem);
- kfree(pcm_buf);
- lab_d->pcm_buf = NULL;
- exit:
- return rc;
- }
- static int msm_cpe_lab_buf_dealloc(struct snd_pcm_substream *substream,
- struct cpe_lsm_session *session, struct msm_slim_dma_data *dma_data)
- {
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct cpe_lsm_lab *lab_d = &lsm_d->lab;
- struct cpe_hw_params *hw_params = &lsm_d->hw_params;
- int rc = 0;
- int dma_alloc = 0;
- struct cpe_data_pcm_buf *pcm_buf = NULL;
- int bufsz, bufcnt;
- bufsz = hw_params->buf_sz;
- bufcnt = hw_params->period_count;
- dev_dbg(rtd->dev,
- "%s:Buf Size %d Buf count %d\n", __func__,
- bufsz, bufcnt);
- if (bufcnt <= 0 || bufsz <= 0) {
- dev_err(rtd->dev,
- "%s: Invalid params, bufsz = %u, bufcnt = %u\n",
- __func__, bufsz, bufcnt);
- return -EINVAL;
- }
- pcm_buf = lab_d->pcm_buf;
- dma_alloc = bufsz * bufcnt;
- if (dma_data && pcm_buf)
- if (pcm_buf->phys)
- dma_unmap_single(dma_data->sdev->dev.parent,
- pcm_buf->phys, dma_alloc, DMA_BIDIRECTIONAL);
- if (pcm_buf)
- kfree(pcm_buf->mem);
- kfree(pcm_buf);
- lab_d->pcm_buf = NULL;
- return rc;
- }
- /*
- * msm_cpe_lab_thread: Initiated on KW detection
- * @data: lab data
- *
- * Start lab thread and call CPE core API for SLIM
- * read operations.
- */
- static int msm_cpe_lab_thread(void *data)
- {
- struct cpe_lsm_data *lsm_d = data;
- struct cpe_lsm_session *session = lsm_d->lsm_session;
- struct snd_pcm_substream *substream = lsm_d->substream;
- struct cpe_lsm_lab *lab_d = &lsm_d->lab;
- struct cpe_hw_params *hw_params = &lsm_d->hw_params;
- struct cpe_priv *cpe = cpe_get_private_data(substream);
- struct wcd_cpe_lsm_ops *lsm_ops;
- struct wcd_cpe_afe_ops *afe_ops;
- struct cpe_data_pcm_buf *cur_buf, *next_buf;
- struct msm_slim_dma_data *dma_data = NULL;
- struct snd_soc_pcm_runtime *rtd = NULL;
- bool wait_timedout = false;
- int rc = 0;
- u32 done_len = 0;
- u32 buf_count = 0;
- u32 prd_cnt;
- allow_signal(SIGKILL);
- set_current_state(TASK_INTERRUPTIBLE);
- pr_debug("%s: Lab thread start\n", __func__);
- init_completion(&lab_d->comp);
- if (PCM_RUNTIME_CHECK(substream)) {
- rc = -EINVAL;
- goto done;
- }
- if (!cpe || !cpe->core_handle) {
- pr_err("%s: Handle to %s is invalid\n",
- __func__,
- (!cpe) ? "cpe" : "core");
- rc = -EINVAL;
- goto done;
- }
- rtd = substream->private_data;
- if (rtd->cpu_dai)
- dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
- substream);
- if (!dma_data || !dma_data->dai_channel_ctl) {
- pr_err("%s: dma_data is not set\n", __func__);
- rc = -EINVAL;
- goto done;
- }
- lsm_ops = &cpe->lsm_ops;
- afe_ops = &cpe->afe_ops;
- rc = lsm_ops->lab_ch_setup(cpe->core_handle,
- session,
- WCD_CPE_PRE_ENABLE);
- if (rc) {
- dev_err(rtd->dev,
- "%s: PRE ch setup failed, err = %d\n",
- __func__, rc);
- goto done;
- }
- rc = dma_data->dai_channel_ctl(dma_data, rtd->cpu_dai, true);
- if (rc) {
- dev_err(rtd->dev,
- "%s: open data failed %d\n", __func__, rc);
- goto done;
- }
- dev_dbg(rtd->dev, "%s: Established data channel\n",
- __func__);
- init_waitqueue_head(&lab_d->period_wait);
- memset(lab_d->pcm_buf[0].mem, 0, lab_d->pcm_size);
- rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
- lab_d->pcm_buf[0].mem,
- hw_params->buf_sz, &lab_d->comp);
- if (rc) {
- dev_err(rtd->dev,
- "%s: buf[0] slim_port_xfer failed, err = %d\n",
- __func__, rc);
- goto done;
- }
- rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
- lab_d->pcm_buf[1].mem,
- hw_params->buf_sz, &lab_d->comp);
- if (rc) {
- dev_err(rtd->dev,
- "%s: buf[0] slim_port_xfer failed, err = %d\n",
- __func__, rc);
- goto done;
- }
- cur_buf = &lab_d->pcm_buf[0];
- next_buf = &lab_d->pcm_buf[2];
- prd_cnt = hw_params->period_count;
- rc = lsm_ops->lab_ch_setup(cpe->core_handle,
- session,
- WCD_CPE_POST_ENABLE);
- if (rc) {
- dev_err(rtd->dev,
- "%s: POST ch setup failed, err = %d\n",
- __func__, rc);
- goto done;
- }
- rc = afe_ops->afe_port_start(cpe->core_handle,
- &session->afe_out_port_cfg);
- if (rc) {
- dev_err(rtd->dev,
- "%s: AFE out port start failed, err = %d\n",
- __func__, rc);
- goto done;
- }
- while (!kthread_should_stop() &&
- lab_d->thread_status != MSM_LSM_LAB_THREAD_ERROR) {
- rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
- next_buf->mem,
- hw_params->buf_sz, &lab_d->comp);
- if (rc) {
- dev_err(rtd->dev,
- "%s: slim_port_xfer failed, err = %d\n",
- __func__, rc);
- lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
- }
- rc = wait_for_completion_timeout(&lab_d->comp, (2 * HZ/10));
- if (!rc) {
- dev_err(rtd->dev,
- "%s: wait timedout for slim buffer\n",
- __func__);
- wait_timedout = true;
- } else {
- wait_timedout = false;
- }
- rc = slim_port_get_xfer_status(dma_data->sdev,
- dma_data->ph,
- &cur_buf->phys, &done_len);
- if (rc ||
- (!rc && wait_timedout)) {
- dev_err(rtd->dev,
- "%s: xfer_status failure, rc = %d, wait_timedout = %s\n",
- __func__, rc,
- (wait_timedout ? "true" : "false"));
- lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
- }
- if (done_len ||
- ((!done_len) &&
- lab_d->thread_status == MSM_LSM_LAB_THREAD_ERROR)) {
- atomic_inc(&lab_d->in_count);
- lab_d->dma_write += snd_pcm_lib_period_bytes(substream);
- snd_pcm_period_elapsed(substream);
- wake_up(&lab_d->period_wait);
- buf_count++;
- cur_buf = &lab_d->pcm_buf[buf_count % prd_cnt];
- next_buf = &lab_d->pcm_buf[(buf_count + 2) % prd_cnt];
- dev_dbg(rtd->dev,
- "%s: Cur buf.mem = %pK Next Buf.mem = %pK\n"
- " buf count = 0x%x\n", __func__,
- cur_buf->mem, next_buf->mem, buf_count);
- } else {
- dev_err(rtd->dev,
- "%s: SB get status, invalid len = 0x%x\n",
- __func__, done_len);
- }
- done_len = 0;
- }
- done:
- if (rc)
- lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
- pr_debug("%s: Exit lab_thread, exit_status=%d, thread_status=%d\n",
- __func__, rc, lab_d->thread_status);
- complete(&lab_d->thread_complete);
- return 0;
- }
- /*
- * msm_cpe_lsm_open: ASoC call to open the stream
- * @substream: substream that is to be opened
- *
- * Create session data for lsm session and open the lsm session
- * on CPE.
- */
- static int msm_cpe_lsm_open(struct snd_pcm_substream *substream)
- {
- struct cpe_lsm_data *lsm_d;
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_priv *cpe = cpe_get_private_data(substream);
- struct wcd_cpe_lsm_ops *lsm_ops;
- int rc = 0;
- if (!cpe || !cpe->codec) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- runtime->hw = msm_pcm_hardware_listen;
- rc = snd_pcm_hw_constraint_list(runtime, 0,
- SNDRV_PCM_HW_PARAM_RATE,
- &constraints_sample_rates);
- if (rc < 0) {
- pr_err("snd_pcm_hw_constraint_list failed rc %d\n", rc);
- return -EINVAL;
- }
- /* Ensure that buffer size is a multiple of period size */
- rc = snd_pcm_hw_constraint_integer(runtime,
- SNDRV_PCM_HW_PARAM_PERIODS);
- if (rc < 0) {
- pr_err("%s: Unable to set pcm_param_periods, rc %d\n",
- __func__, rc);
- return -EINVAL;
- }
- rc = snd_pcm_hw_constraint_minmax(runtime,
- SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
- LISTEN_MIN_NUM_PERIODS * LISTEN_MIN_PERIOD_SIZE,
- LISTEN_MAX_NUM_PERIODS * LISTEN_MAX_PERIOD_SIZE);
- if (rc < 0) {
- pr_err("%s: Unable to set pcm constraints, rc %d\n",
- __func__, rc);
- return -EINVAL;
- }
- cpe->core_handle = wcd_cpe_get_core_handle(cpe->codec);
- if (!cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid handle to codec core\n",
- __func__);
- return -EINVAL;
- }
- lsm_ops = &cpe->lsm_ops;
- lsm_d = kzalloc(sizeof(struct cpe_lsm_data), GFP_KERNEL);
- if (!lsm_d) {
- dev_err(rtd->dev,
- "%s: ENOMEM for lsm session, size = %zd\n",
- __func__, sizeof(struct cpe_lsm_data));
- rc = -ENOMEM;
- goto fail_return;
- }
- mutex_init(&lsm_d->lsm_api_lock);
- lsm_d->lsm_session = lsm_ops->lsm_alloc_session(cpe->core_handle,
- lsm_d, msm_cpe_process_event_status);
- if (!lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: session allocation failed",
- __func__);
- rc = -EINVAL;
- goto fail_session_alloc;
- }
- /* Explicitly Assign the LAB thread to STOP state */
- lsm_d->lab.thread_status = MSM_LSM_LAB_THREAD_STOP;
- lsm_d->lsm_session->started = false;
- lsm_d->substream = substream;
- init_waitqueue_head(&lsm_d->lab.period_wait);
- lsm_d->cpe_prepared = false;
- dev_dbg(rtd->dev, "%s: allocated session with id = %d\n",
- __func__, lsm_d->lsm_session->id);
- rc = lsm_ops->lsm_open_tx(cpe->core_handle, lsm_d->lsm_session,
- LSM_VOICE_WAKEUP_APP_V2, 16000);
- if (rc < 0) {
- dev_err(rtd->dev,
- "%s: OPEN_TX cmd failed, err = %d\n",
- __func__, rc);
- goto fail_open_tx;
- }
- init_waitqueue_head(&lsm_d->event_wait);
- atomic_set(&lsm_d->event_avail, 0);
- atomic_set(&lsm_d->event_stop, 0);
- runtime->private_data = lsm_d;
- return 0;
- fail_open_tx:
- lsm_ops->lsm_dealloc_session(cpe->core_handle, lsm_d->lsm_session);
- fail_session_alloc:
- mutex_destroy(&lsm_d->lsm_api_lock);
- kfree(lsm_d);
- fail_return:
- return rc;
- }
- /*
- * msm_cpe_lsm_close: ASoC call to close/cleanup the stream
- * @substream: substream that is to be closed
- *
- * Deallocate the session and release the AFE port. It is not
- * required to deregister the sound model as long as we close
- * the lsm session on CPE.
- */
- static int msm_cpe_lsm_close(struct snd_pcm_substream *substream)
- {
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct cpe_priv *cpe = cpe_get_private_data(substream);
- struct wcd_cpe_lsm_ops *lsm_ops;
- struct cpe_lsm_session *session;
- struct wcd_cpe_afe_ops *afe_ops;
- struct wcd_cpe_afe_port_cfg *afe_cfg;
- int rc = 0;
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- __func__);
- return -EINVAL;
- }
- lsm_ops = &cpe->lsm_ops;
- session = lsm_d->lsm_session;
- afe_ops = &cpe->afe_ops;
- afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
- /*
- * If driver is closed without stopping LAB,
- * explicitly stop LAB before cleaning up the
- * driver resources.
- */
- rc = msm_cpe_lsm_lab_stop(substream);
- if (rc) {
- dev_err(rtd->dev,
- "%s: Failed to stop lab, error = %d\n",
- __func__, rc);
- return rc;
- }
- rc = msm_cpe_afe_port_cntl(substream,
- cpe->core_handle,
- afe_ops, afe_cfg,
- AFE_CMD_PORT_STOP);
- lsm_d->cpe_prepared = false;
- rc = lsm_ops->lsm_close_tx(cpe->core_handle, session);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: lsm_close fail, err = %d\n",
- __func__, rc);
- return rc;
- }
- lsm_ops->lsm_dealloc_session(cpe->core_handle, session);
- runtime->private_data = NULL;
- mutex_destroy(&lsm_d->lsm_api_lock);
- kfree(lsm_d);
- return rc;
- }
- static int msm_cpe_lsm_get_conf_levels(
- struct cpe_lsm_session *session,
- u8 *conf_levels_ptr)
- {
- int rc = 0;
- if (session->num_confidence_levels <= 0) {
- pr_debug("%s: conf_levels (%u), skip set params\n",
- __func__,
- session->num_confidence_levels);
- goto done;
- }
- session->conf_levels = kzalloc(session->num_confidence_levels,
- GFP_KERNEL);
- if (!session->conf_levels) {
- rc = -ENOMEM;
- goto done;
- }
- if (copy_from_user(session->conf_levels,
- conf_levels_ptr,
- session->num_confidence_levels)) {
- pr_err("%s: copy_from_user failed for confidence levels %u\n",
- __func__, session->num_confidence_levels);
- kfree(session->conf_levels);
- session->conf_levels = NULL;
- rc = -EFAULT;
- goto done;
- }
- done:
- return rc;
- }
- static int msm_cpe_lsm_validate_out_format(
- struct snd_pcm_substream *substream,
- struct snd_lsm_output_format_cfg *cfg)
- {
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- int rc = 0;
- if (!cfg) {
- dev_err(rtd->dev,
- "%s: Invalid lsm out cfg\n", __func__);
- rc = -EINVAL;
- goto done;
- }
- if (cfg->format != LSM_OUT_FORMAT_PCM &&
- cfg->format != LSM_OUT_FORMAT_ADPCM) {
- dev_err(rtd->dev,
- "%s: Invalid format %u\n",
- __func__, cfg->format);
- rc = -EINVAL;
- goto done;
- }
- if (cfg->packing != LSM_OUT_DATA_RAW &&
- cfg->packing != LSM_OUT_DATA_PACKED) {
- dev_err(rtd->dev,
- "%s: Invalid packing method %u\n",
- __func__, cfg->packing);
- rc = -EINVAL;
- goto done;
- }
- if (cfg->events != LSM_OUT_DATA_EVENTS_DISABLED &&
- cfg->events != LSM_OUT_DATA_EVENTS_ENABLED) {
- dev_err(rtd->dev,
- "%s: Invalid events provided %u\n",
- __func__, cfg->events);
- rc = -EINVAL;
- goto done;
- }
- if (cfg->mode != LSM_OUT_TRANSFER_MODE_RT &&
- cfg->mode != LSM_OUT_TRANSFER_MODE_FTRT) {
- dev_err(rtd->dev,
- "%s: Invalid transfer mode %u\n",
- __func__, cfg->mode);
- rc = -EINVAL;
- goto done;
- }
- done:
- return rc;
- }
- /*
- * msm_cpe_lsm_ioctl_shared: Shared IOCTL for this platform driver
- * @substream: ASoC substream for which the operation is invoked
- * @cmd: command for the ioctl
- * @arg: argument for the ioctl
- *
- * Perform dedicated listen functions like register sound model,
- * deregister sound model, etc
- * Called with lsm_api_lock acquired.
- */
- static int msm_cpe_lsm_ioctl_shared(struct snd_pcm_substream *substream,
- unsigned int cmd, void *arg)
- {
- struct snd_lsm_sound_model_v2 snd_model;
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct cpe_priv *cpe = cpe_get_private_data(substream);
- struct cpe_lsm_session *session;
- struct wcd_cpe_lsm_ops *lsm_ops;
- struct cpe_lsm_lab *lab_d = &lsm_d->lab;
- struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
- struct msm_slim_dma_data *dma_data = NULL;
- struct snd_lsm_detection_params det_params;
- int rc = 0;
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- __func__);
- return -EINVAL;
- }
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- switch (cmd) {
- case SNDRV_LSM_STOP_LAB:
- dev_dbg(rtd->dev,
- "%s: %s, lab_enable = %d, lab_thread_ststus = %d\n",
- __func__, "SNDRV_LSM_STOP_LAB",
- session->lab_enable,
- lab_d->thread_status);
- if (session->lab_enable &&
- lab_d->thread_status != MSM_LSM_LAB_THREAD_STOP) {
- atomic_inc(&lab_d->abort_read);
- wake_up(&lab_d->period_wait);
- rc = msm_cpe_lsm_lab_stop(substream);
- if (rc) {
- dev_err(rtd->dev,
- "%s: stop LAB failed, error = %d\n",
- __func__, rc);
- return rc;
- }
- } else if (!session->lab_enable) {
- dev_dbg(rtd->dev,
- "%s: LAB already stopped\n",
- __func__);
- }
- break;
- case SNDRV_LSM_LAB_CONTROL:
- if (copy_from_user(&session->lab_enable, (void *)arg,
- sizeof(u32))) {
- dev_err(rtd->dev,
- "%s: copy_from_user failed, size %zd\n",
- __func__, sizeof(u32));
- return -EFAULT;
- }
- dev_dbg(rtd->dev,
- "%s: %s, lab_enable = %d\n",
- __func__, "SNDRV_LSM_LAB_CONTROL",
- session->lab_enable);
- if (rtd->cpu_dai)
- dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
- substream);
- if (!dma_data || !dma_data->dai_channel_ctl) {
- dev_err(rtd->dev,
- "%s: dma_data is not set\n", __func__);
- return -EINVAL;
- }
- if (session->lab_enable) {
- rc = msm_cpe_lab_buf_alloc(substream,
- session, dma_data);
- if (rc < 0) {
- dev_err(rtd->dev,
- "%s: lab buffer alloc failed, err = %d\n",
- __func__, rc);
- return rc;
- }
- dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
- dma_buf->dev.dev = substream->pcm->card->dev;
- dma_buf->private_data = NULL;
- dma_buf->area = lab_d->pcm_buf[0].mem;
- dma_buf->addr = lab_d->pcm_buf[0].phys;
- dma_buf->bytes = (lsm_d->hw_params.buf_sz *
- lsm_d->hw_params.period_count);
- init_completion(&lab_d->thread_complete);
- snd_pcm_set_runtime_buffer(substream,
- &substream->dma_buffer);
- rc = lsm_ops->lsm_lab_control(cpe->core_handle,
- session, true);
- if (rc < 0) {
- dev_err(rtd->dev,
- "%s: Lab Enable Failed rc %d\n",
- __func__, rc);
- return rc;
- }
- } else {
- /*
- * It is possible that lab is still enabled
- * when trying to de-allocate the lab buffer.
- * Make sure to disable lab before de-allocating
- * the lab buffer.
- */
- rc = msm_cpe_lsm_lab_stop(substream);
- if (rc < 0) {
- dev_err(rtd->dev,
- "%s: LAB stop failed, error = %d\n",
- __func__, rc);
- return rc;
- }
- /*
- * Buffer has to be de-allocated even if
- * lab_control failed.
- */
- rc = msm_cpe_lab_buf_dealloc(substream,
- session, dma_data);
- if (rc < 0) {
- dev_err(rtd->dev,
- "%s: lab buffer free failed, err = %d\n",
- __func__, rc);
- return rc;
- }
- }
- break;
- case SNDRV_LSM_REG_SND_MODEL_V2:
- dev_dbg(rtd->dev,
- "%s: %s\n",
- __func__, "SNDRV_LSM_REG_SND_MODEL_V2");
- memcpy(&snd_model, arg,
- sizeof(struct snd_lsm_sound_model_v2));
- session->num_confidence_levels =
- snd_model.num_confidence_levels;
- rc = msm_cpe_lsm_get_conf_levels(session,
- snd_model.confidence_level);
- if (rc) {
- dev_err(rtd->dev,
- "%s: %s get_conf_levels fail, err = %d\n",
- __func__, "SNDRV_LSM_REG_SND_MODEL_V2",
- rc);
- break;
- }
- session->snd_model_data = kzalloc(snd_model.data_size,
- GFP_KERNEL);
- if (!session->snd_model_data) {
- kfree(session->conf_levels);
- session->conf_levels = NULL;
- return -ENOMEM;
- }
- session->snd_model_size = snd_model.data_size;
- if (copy_from_user(session->snd_model_data,
- snd_model.data, snd_model.data_size)) {
- dev_err(rtd->dev,
- "%s: copy_from_user failed for snd_model\n",
- __func__);
- kfree(session->conf_levels);
- kfree(session->snd_model_data);
- session->conf_levels = NULL;
- session->snd_model_data = NULL;
- return -EFAULT;
- }
- rc = lsm_ops->lsm_shmem_alloc(cpe->core_handle, session,
- session->snd_model_size);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: shared memory allocation failed, err = %d\n",
- __func__, rc);
- kfree(session->snd_model_data);
- kfree(session->conf_levels);
- session->snd_model_data = NULL;
- session->conf_levels = NULL;
- return rc;
- }
- rc = lsm_ops->lsm_register_snd_model(cpe->core_handle, session,
- snd_model.detection_mode,
- snd_model.detect_failure);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: snd_model_reg failed, err = %d\n",
- __func__, rc);
- lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
- kfree(session->snd_model_data);
- kfree(session->conf_levels);
- session->snd_model_data = NULL;
- session->conf_levels = NULL;
- return rc;
- }
- break;
- case SNDRV_LSM_DEREG_SND_MODEL:
- dev_dbg(rtd->dev,
- "%s: %s\n",
- __func__, "SNDRV_LSM_DEREG_SND_MODEL");
- if (session->lab_enable) {
- /*
- * It is possible that lab is still enabled
- * when trying to deregister sound model.
- * Make sure to disable lab before de-allocating
- * the lab buffer.
- */
- rc = msm_cpe_lsm_lab_stop(substream);
- if (rc) {
- dev_err(rtd->dev,
- "%s: LAB stop failed, error = %d\n",
- __func__, rc);
- return rc;
- }
- rc = lsm_ops->lsm_lab_control(cpe->core_handle,
- session, false);
- if (rc)
- dev_err(rtd->dev,
- "%s: Lab Disable Failed rc %d\n",
- __func__, rc);
- dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
- substream);
- if (!dma_data || !dma_data->dai_channel_ctl)
- dev_err(rtd->dev,
- "%s: dma_data is not set\n", __func__);
- /*
- * Buffer has to be de-allocated even if
- * lab_control failed and/or dma data is invalid.
- */
- rc = msm_cpe_lab_buf_dealloc(substream,
- session, dma_data);
- if (rc < 0)
- dev_err(rtd->dev,
- "%s: lab buffer free failed, err = %d\n",
- __func__, rc);
- }
- rc = lsm_ops->lsm_deregister_snd_model(
- cpe->core_handle, session);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: snd_model de-reg failed, err = %d\n",
- __func__, rc);
- return rc;
- }
- kfree(session->snd_model_data);
- kfree(session->conf_levels);
- session->snd_model_data = NULL;
- session->conf_levels = NULL;
- rc = lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: LSM shared memory dealloc failed, err = %d\n",
- __func__, rc);
- return rc;
- }
- break;
- case SNDRV_LSM_EVENT_STATUS:
- case SNDRV_LSM_EVENT_STATUS_V3: {
- struct snd_lsm_event_status *user;
- struct snd_lsm_event_status_v3 *user_v3;
- dev_dbg(rtd->dev,
- "%s: %s\n",
- __func__, "SNDRV_LSM_EVENT_STATUS(_V3)");
- if (!arg) {
- dev_err(rtd->dev,
- "%s: Invalid argument to ioctl %s\n",
- __func__,
- "SNDRV_LSM_EVENT_STATUS(_V3)");
- return -EINVAL;
- }
- /*
- * Release the api lock before wait to allow
- * other IOCTLs to be invoked while waiting
- * for event
- */
- MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
- "lsm_api_lock");
- rc = wait_event_freezable(lsm_d->event_wait,
- (atomic_read(&lsm_d->event_avail) == 1) ||
- (atomic_read(&lsm_d->event_stop) == 1));
- MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
- "lsm_api_lock");
- if (!rc) {
- if (atomic_read(&lsm_d->event_avail) == 1) {
- rc = 0;
- atomic_set(&lsm_d->event_avail, 0);
- if (cmd == SNDRV_LSM_EVENT_STATUS) {
- user = arg;
- if (lsm_d->ev_det_pld_size >
- user->payload_size) {
- dev_err(rtd->dev,
- "%s: avail pld_bytes = %u, needed = %u\n",
- __func__,
- user->payload_size,
- lsm_d->ev_det_pld_size);
- return -EINVAL;
- }
- user->status = lsm_d->ev_det_status;
- user->payload_size =
- lsm_d->ev_det_pld_size;
- memcpy(user->payload,
- lsm_d->ev_det_payload,
- lsm_d->ev_det_pld_size);
- } else {
- user_v3 = arg;
- if (lsm_d->ev_det_pld_size >
- user_v3->payload_size) {
- dev_err(rtd->dev,
- "%s: avail pld_bytes = %u, needed = %u\n",
- __func__,
- user_v3->payload_size,
- lsm_d->ev_det_pld_size);
- return -EINVAL;
- }
- /* event status timestamp not supported
- * on CPE mode. Set msw and lsw to 0.
- */
- user_v3->timestamp_lsw = 0;
- user_v3->timestamp_msw = 0;
- user_v3->status = lsm_d->ev_det_status;
- user_v3->payload_size =
- lsm_d->ev_det_pld_size;
- memcpy(user_v3->payload,
- lsm_d->ev_det_payload,
- lsm_d->ev_det_pld_size);
- }
- } else if (atomic_read(&lsm_d->event_stop) == 1) {
- dev_dbg(rtd->dev,
- "%s: wait_aborted\n", __func__);
- if (cmd == SNDRV_LSM_EVENT_STATUS) {
- user = arg;
- user->payload_size = 0;
- } else {
- user_v3 = arg;
- user_v3->payload_size = 0;
- }
- rc = 0;
- }
- }
- }
- break;
- case SNDRV_LSM_ABORT_EVENT:
- dev_dbg(rtd->dev,
- "%s: %s\n",
- __func__, "SNDRV_LSM_ABORT_EVENT");
- atomic_set(&lsm_d->event_stop, 1);
- wake_up(&lsm_d->event_wait);
- break;
- case SNDRV_LSM_START:
- dev_dbg(rtd->dev,
- "%s: %s\n",
- __func__, "SNDRV_LSM_START");
- rc = lsm_ops->lsm_start(cpe->core_handle, session);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: lsm_start fail, err = %d\n",
- __func__, rc);
- return rc;
- }
- session->started = true;
- break;
- case SNDRV_LSM_STOP:
- dev_dbg(rtd->dev,
- "%s: %s, lab_enable = %d, lab_thread_status = %d\n",
- __func__, "SNDRV_LSM_STOP",
- session->lab_enable,
- lab_d->thread_status);
- if ((session->lab_enable &&
- lab_d->thread_status ==
- MSM_LSM_LAB_THREAD_RUNNING)) {
- /* Explicitly stop LAB */
- rc = msm_cpe_lsm_lab_stop(substream);
- if (rc) {
- dev_err(rtd->dev,
- "%s: lab_stop failed, err = %d\n",
- __func__, rc);
- return rc;
- }
- }
- rc = lsm_ops->lsm_stop(cpe->core_handle, session);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: lsm_stop fail err = %d\n",
- __func__, rc);
- return rc;
- }
- session->started = false;
- break;
- case SNDRV_LSM_SET_PARAMS:
- memcpy(&det_params, arg,
- sizeof(det_params));
- if (det_params.num_confidence_levels <= 0) {
- dev_err(rtd->dev,
- "%s: %s: Invalid confidence levels %u\n",
- __func__, "SNDRV_LSM_SET_PARAMS",
- det_params.num_confidence_levels);
- return -EINVAL;
- }
- session->num_confidence_levels =
- det_params.num_confidence_levels;
- rc = msm_cpe_lsm_get_conf_levels(session,
- det_params.conf_level);
- if (rc) {
- dev_err(rtd->dev,
- "%s: %s get_conf_levels fail, err = %d\n",
- __func__, "SNDRV_LSM_SET_PARAMS",
- rc);
- break;
- }
- rc = lsm_ops->lsm_set_data(cpe->core_handle, session,
- det_params.detect_mode,
- det_params.detect_failure);
- if (rc) {
- dev_err(rtd->dev,
- "%s: lsm_set_data failed, err = %d\n",
- __func__, rc);
- return rc;
- }
- kfree(session->conf_levels);
- session->conf_levels = NULL;
- break;
- case SNDRV_LSM_OUT_FORMAT_CFG: {
- struct snd_lsm_output_format_cfg u_fmt_cfg;
- if (!arg) {
- dev_err(rtd->dev,
- "%s: Invalid argument to ioctl %s\n",
- __func__, "SNDRV_LSM_OUT_FORMAT_CFG");
- return -EINVAL;
- }
- if (copy_from_user(&u_fmt_cfg, arg,
- sizeof(u_fmt_cfg))) {
- dev_err(rtd->dev,
- "%s: copy_from_user failed for out_fmt_cfg\n",
- __func__);
- return -EFAULT;
- }
- if (msm_cpe_lsm_validate_out_format(substream,
- &u_fmt_cfg))
- return -EINVAL;
- session->out_fmt_cfg.format = u_fmt_cfg.format;
- session->out_fmt_cfg.pack_mode = u_fmt_cfg.packing;
- session->out_fmt_cfg.data_path_events =
- u_fmt_cfg.events;
- session->out_fmt_cfg.transfer_mode = u_fmt_cfg.mode;
- rc = lsm_ops->lsm_set_fmt_cfg(cpe->core_handle,
- session);
- if (rc) {
- dev_err(rtd->dev,
- "%s: lsm_set_fmt_cfg failed, err = %d\n",
- __func__, rc);
- return rc;
- }
- }
- break;
- case SNDRV_LSM_SET_PORT: {
- u32 port_id = cpe->input_port_id;
- dev_dbg(rtd->dev, "%s: %s\n", __func__, "SNDRV_LSM_SET_PORT");
- rc = lsm_ops->lsm_set_port(cpe->core_handle, session, &port_id);
- if (rc) {
- dev_err(rtd->dev,
- "%s: lsm_set_port failed, err = %d\n",
- __func__, rc);
- return rc;
- }
- }
- break;
- default:
- dev_dbg(rtd->dev,
- "%s: Default snd_lib_ioctl cmd 0x%x\n",
- __func__, cmd);
- rc = snd_pcm_lib_ioctl(substream, cmd, arg);
- }
- return rc;
- }
- static int msm_cpe_lsm_lab_start(struct snd_pcm_substream *substream,
- u16 event_det_status)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct cpe_lsm_lab *lab_d = NULL;
- struct cpe_hw_params *hw_params;
- struct wcd_cpe_lsm_ops *lsm_ops;
- struct wcd_cpe_afe_ops *afe_ops;
- struct wcd_cpe_afe_port_cfg *out_port;
- int rc;
- if (!substream || !substream->private_data) {
- pr_err("%s: invalid substream (%pK)\n",
- __func__, substream);
- return -EINVAL;
- }
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- __func__);
- return -EINVAL;
- }
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- lab_d = &lsm_d->lab;
- afe_ops = &cpe->afe_ops;
- hw_params = &lsm_d->hw_params;
- if (!session->started) {
- dev_dbg(rtd->dev,
- "%s: Session is stopped, cannot start LAB\n",
- __func__);
- return 0;
- }
- reinit_completion(&lab_d->thread_complete);
- if (session->lab_enable &&
- event_det_status ==
- LSM_VOICE_WAKEUP_STATUS_DETECTED) {
- out_port = &session->afe_out_port_cfg;
- out_port->port_id = session->afe_out_port_id;
- out_port->bit_width = hw_params->sample_size;
- out_port->num_channels = hw_params->channels;
- out_port->sample_rate = hw_params->sample_rate;
- dev_dbg(rtd->dev, "%s: port_id= %u, bit_width= %u, rate= %u\n",
- __func__, out_port->port_id, out_port->bit_width,
- out_port->sample_rate);
- rc = afe_ops->afe_port_cmd_cfg(cpe->core_handle,
- out_port);
- if (rc) {
- dev_err(rtd->dev,
- "%s: Failed afe generic config v2, err = %d\n",
- __func__, rc);
- return rc;
- }
- atomic_set(&lab_d->abort_read, 0);
- dev_dbg(rtd->dev,
- "%s: KW detected, scheduling LAB thread\n",
- __func__);
- /*
- * Even though thread might be only scheduled and
- * not currently running, mark the internal driver
- * status to running so driver can cancel this thread
- * if it needs to before the thread gets chance to run.
- */
- lab_d->thread_status = MSM_LSM_LAB_THREAD_RUNNING;
- session->lsm_lab_thread = kthread_run(
- msm_cpe_lab_thread,
- lsm_d,
- "lab_thread");
- }
- return 0;
- }
- static bool msm_cpe_lsm_is_valid_stream(struct snd_pcm_substream *substream,
- const char *func)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- if (!substream || !substream->private_data) {
- pr_err("%s: invalid substream (%pK)\n",
- func, substream);
- return false;
- }
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- func);
- return false;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- func);
- return false;
- }
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- if (!lsm_ops) {
- dev_err(rtd->dev,
- "%s: Invalid lsm_ops\n", func);
- return false;
- }
- return true;
- }
- static int msm_cpe_lsm_set_epd(struct snd_pcm_substream *substream,
- struct lsm_params_info *p_info)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- struct snd_lsm_ep_det_thres epd_thres;
- int rc;
- if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
- return -EINVAL;
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- if (p_info->param_size != sizeof(epd_thres)) {
- dev_err(rtd->dev,
- "%s: Invalid param_size %d\n",
- __func__, p_info->param_size);
- rc = -EINVAL;
- goto done;
- }
- if (copy_from_user(&epd_thres, p_info->param_data,
- p_info->param_size)) {
- dev_err(rtd->dev,
- "%s: copy_from_user failed, size = %d\n",
- __func__, p_info->param_size);
- rc = -EFAULT;
- goto done;
- }
- rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
- session, p_info, &epd_thres,
- LSM_ENDPOINT_DETECT_THRESHOLD);
- if (unlikely(rc))
- dev_err(rtd->dev,
- "%s: set_one_param(epd_threshold) failed, rc %d\n",
- __func__, rc);
- done:
- return rc;
- }
- static int msm_cpe_lsm_set_mode(struct snd_pcm_substream *substream,
- struct lsm_params_info *p_info)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- struct snd_lsm_detect_mode det_mode;
- int rc;
- if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
- return -EINVAL;
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- if (p_info->param_size != sizeof(det_mode)) {
- dev_err(rtd->dev,
- "%s: Invalid param_size %d\n",
- __func__, p_info->param_size);
- rc = -EINVAL;
- goto done;
- }
- if (copy_from_user(&det_mode, p_info->param_data,
- p_info->param_size)) {
- dev_err(rtd->dev,
- "%s: copy_from_user failed, size = %d\n",
- __func__, p_info->param_size);
- rc = -EFAULT;
- goto done;
- }
- rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
- session, p_info, &det_mode,
- LSM_OPERATION_MODE);
- if (unlikely(rc))
- dev_err(rtd->dev,
- "%s: set_one_param(epd_threshold) failed, rc %d\n",
- __func__, rc);
- done:
- return rc;
- }
- static int msm_cpe_lsm_set_gain(struct snd_pcm_substream *substream,
- struct lsm_params_info *p_info)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- struct snd_lsm_gain gain;
- int rc;
- if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
- return -EINVAL;
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- if (p_info->param_size != sizeof(gain)) {
- dev_err(rtd->dev,
- "%s: Invalid param_size %d\n",
- __func__, p_info->param_size);
- rc = -EINVAL;
- goto done;
- }
- if (copy_from_user(&gain, p_info->param_data,
- p_info->param_size)) {
- dev_err(rtd->dev,
- "%s: copy_from_user failed, size = %d\n",
- __func__, p_info->param_size);
- rc = -EFAULT;
- goto done;
- }
- rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
- session, p_info, &gain,
- LSM_GAIN);
- if (unlikely(rc))
- dev_err(rtd->dev,
- "%s: set_one_param(epd_threshold) failed, rc %d\n",
- __func__, rc);
- done:
- return rc;
- }
- static int msm_cpe_lsm_set_conf(struct snd_pcm_substream *substream,
- struct lsm_params_info *p_info)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- int rc;
- if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
- return -EINVAL;
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- session->num_confidence_levels =
- p_info->param_size;
- rc = msm_cpe_lsm_get_conf_levels(session,
- p_info->param_data);
- if (rc) {
- dev_err(rtd->dev,
- "%s: get_conf_levels failed, err = %d\n",
- __func__, rc);
- goto done;
- }
- rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
- session, p_info, NULL,
- LSM_MIN_CONFIDENCE_LEVELS);
- if (unlikely(rc))
- dev_err(rtd->dev,
- "%s: set_one_param(conf_levels) failed, rc %d\n",
- __func__, rc);
- done:
- return rc;
- }
- static int msm_cpe_lsm_reg_model(struct snd_pcm_substream *substream,
- struct lsm_params_info *p_info)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- int rc;
- size_t offset;
- u8 *snd_model_ptr;
- if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
- return -EINVAL;
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- lsm_ops->lsm_get_snd_model_offset(cpe->core_handle,
- session, &offset);
- /* Check if 'p_info->param_size + offset' crosses U32_MAX. */
- if (p_info->param_size > U32_MAX - offset) {
- dev_err(rtd->dev,
- "%s: Invalid param_size %d\n",
- __func__, p_info->param_size);
- return -EINVAL;
- }
- session->snd_model_size = p_info->param_size + offset;
- session->snd_model_data = vzalloc(session->snd_model_size);
- if (!session->snd_model_data)
- return -ENOMEM;
- snd_model_ptr = ((u8 *) session->snd_model_data) + offset;
- if (copy_from_user(snd_model_ptr,
- p_info->param_data, p_info->param_size)) {
- dev_err(rtd->dev,
- "%s: copy_from_user for snd_model failed\n",
- __func__);
- rc = -EFAULT;
- goto free_snd_model_data;
- }
- rc = lsm_ops->lsm_shmem_alloc(cpe->core_handle, session,
- session->snd_model_size);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: shared memory allocation failed, err = %d\n",
- __func__, rc);
- rc = -EINVAL;
- goto free_snd_model_data;
- }
- rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
- session, p_info, NULL,
- LSM_REG_SND_MODEL);
- if (unlikely(rc)) {
- dev_err(rtd->dev,
- "%s: set_one_param(snd_model) failed, rc %d\n",
- __func__, rc);
- goto dealloc_shmem;
- }
- return 0;
- dealloc_shmem:
- lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
- free_snd_model_data:
- vfree(session->snd_model_data);
- return rc;
- }
- static int msm_cpe_lsm_dereg_model(struct snd_pcm_substream *substream,
- struct lsm_params_info *p_info)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- int rc;
- if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
- return -EINVAL;
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
- session, p_info, NULL,
- LSM_DEREG_SND_MODEL);
- if (rc)
- dev_err(rtd->dev,
- "%s: dereg_snd_model failed\n",
- __func__);
- return lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
- }
- static int msm_cpe_lsm_set_custom(struct snd_pcm_substream *substream,
- struct lsm_params_info *p_info)
- {
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- u8 *data;
- int rc;
- if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
- return -EINVAL;
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- if (p_info->param_size > MSM_CPE_MAX_CUSTOM_PARAM_SIZE) {
- dev_err(rtd->dev,
- "%s: invalid size %d, max allowed %d\n",
- __func__, p_info->param_size,
- MSM_CPE_MAX_CUSTOM_PARAM_SIZE);
- return -EINVAL;
- }
- data = kzalloc(p_info->param_size, GFP_KERNEL);
- if (!data)
- return -ENOMEM;
- if (copy_from_user(data, p_info->param_data,
- p_info->param_size)) {
- dev_err(rtd->dev,
- "%s: copy_from_user failed for custom params, size = %d\n",
- __func__, p_info->param_size);
- rc = -EFAULT;
- goto err_ret;
- }
- rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
- session, p_info, data,
- LSM_CUSTOM_PARAMS);
- if (rc)
- dev_err(rtd->dev,
- "%s: custom_params failed, err = %d\n",
- __func__, rc);
- err_ret:
- kfree(data);
- return rc;
- }
- static int msm_cpe_lsm_process_params(struct snd_pcm_substream *substream,
- struct snd_lsm_module_params *p_data,
- void *params)
- {
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct lsm_params_info *p_info;
- int i;
- int rc = 0;
- p_info = (struct lsm_params_info *) params;
- for (i = 0; i < p_data->num_params; i++) {
- dev_dbg(rtd->dev,
- "%s: param (%d), module_id = 0x%x, param_id = 0x%x, param_size = 0x%x, param_type = 0x%x\n",
- __func__, i, p_info->module_id,
- p_info->param_id, p_info->param_size,
- p_info->param_type);
- switch (p_info->param_type) {
- case LSM_ENDPOINT_DETECT_THRESHOLD:
- rc = msm_cpe_lsm_set_epd(substream, p_info);
- break;
- case LSM_OPERATION_MODE:
- rc = msm_cpe_lsm_set_mode(substream, p_info);
- break;
- case LSM_GAIN:
- rc = msm_cpe_lsm_set_gain(substream, p_info);
- break;
- case LSM_MIN_CONFIDENCE_LEVELS:
- rc = msm_cpe_lsm_set_conf(substream, p_info);
- break;
- case LSM_REG_SND_MODEL:
- rc = msm_cpe_lsm_reg_model(substream, p_info);
- break;
- case LSM_DEREG_SND_MODEL:
- rc = msm_cpe_lsm_dereg_model(substream, p_info);
- break;
- case LSM_CUSTOM_PARAMS:
- rc = msm_cpe_lsm_set_custom(substream, p_info);
- break;
- default:
- dev_err(rtd->dev,
- "%s: Invalid param_type %d\n",
- __func__, p_info->param_type);
- rc = -EINVAL;
- break;
- }
- if (rc) {
- pr_err("%s: set_param fail for param_type %d\n",
- __func__, p_info->param_type);
- return rc;
- }
- p_info++;
- }
- return rc;
- }
- static int msm_cpe_lsm_ioctl(struct snd_pcm_substream *substream,
- unsigned int cmd, void *arg)
- {
- int err = 0;
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- if (!substream || !substream->private_data) {
- pr_err("%s: invalid substream (%pK)\n",
- __func__, substream);
- return -EINVAL;
- }
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- __func__);
- return -EINVAL;
- }
- MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
- "lsm_api_lock");
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- switch (cmd) {
- case SNDRV_LSM_REG_SND_MODEL_V2: {
- struct snd_lsm_sound_model_v2 snd_model;
- if (session->is_topology_used) {
- dev_err(rtd->dev,
- "%s: %s: not supported if using topology\n",
- __func__, "LSM_REG_SND_MODEL_V2");
- err = -EINVAL;
- goto done;
- }
- if (copy_from_user(&snd_model, (void *)arg,
- sizeof(struct snd_lsm_sound_model_v2))) {
- dev_err(rtd->dev,
- "%s: copy from user failed, size %zd\n",
- __func__,
- sizeof(struct snd_lsm_sound_model_v2));
- err = -EFAULT;
- goto done;
- }
- err = msm_cpe_lsm_ioctl_shared(substream, cmd,
- &snd_model);
- }
- break;
- case SNDRV_LSM_EVENT_STATUS: {
- struct snd_lsm_event_status u_event_status;
- struct snd_lsm_event_status *event_status = NULL;
- int u_pld_size = 0;
- if (copy_from_user(&u_event_status, (void *)arg,
- sizeof(struct snd_lsm_event_status))) {
- dev_err(rtd->dev,
- "%s: event status copy from user failed, size %zd\n",
- __func__,
- sizeof(struct snd_lsm_event_status));
- err = -EFAULT;
- goto done;
- }
- if (u_event_status.payload_size >
- LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
- dev_err(rtd->dev,
- "%s: payload_size %d is invalid, max allowed = %d\n",
- __func__, u_event_status.payload_size,
- LISTEN_MAX_STATUS_PAYLOAD_SIZE);
- err = -EINVAL;
- goto done;
- }
- u_pld_size = sizeof(struct snd_lsm_event_status) +
- u_event_status.payload_size;
- event_status = kzalloc(u_pld_size, GFP_KERNEL);
- if (!event_status) {
- err = -ENOMEM;
- goto done;
- } else {
- event_status->payload_size =
- u_event_status.payload_size;
- err = msm_cpe_lsm_ioctl_shared(substream,
- cmd, event_status);
- }
- if (!err && copy_to_user(arg, event_status, u_pld_size)) {
- dev_err(rtd->dev,
- "%s: copy to user failed\n",
- __func__);
- kfree(event_status);
- err = -EFAULT;
- goto done;
- }
- msm_cpe_lsm_lab_start(substream, event_status->status);
- msm_cpe_process_event_status_done(lsm_d);
- kfree(event_status);
- }
- break;
- case SNDRV_LSM_EVENT_STATUS_V3: {
- struct snd_lsm_event_status_v3 u_event_status;
- struct snd_lsm_event_status_v3 *event_status = NULL;
- int u_pld_size = 0;
- if (copy_from_user(&u_event_status, (void *)arg,
- sizeof(struct snd_lsm_event_status_v3))) {
- dev_err(rtd->dev,
- "%s: event status copy from user failed, size %zd\n",
- __func__,
- sizeof(struct snd_lsm_event_status_v3));
- err = -EFAULT;
- goto done;
- }
- if (u_event_status.payload_size >
- LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
- dev_err(rtd->dev,
- "%s: payload_size %d is invalid, max allowed = %d\n",
- __func__, u_event_status.payload_size,
- LISTEN_MAX_STATUS_PAYLOAD_SIZE);
- err = -EINVAL;
- goto done;
- }
- u_pld_size = sizeof(struct snd_lsm_event_status_v3) +
- u_event_status.payload_size;
- event_status = kzalloc(u_pld_size, GFP_KERNEL);
- if (!event_status) {
- err = -ENOMEM;
- goto done;
- } else {
- event_status->payload_size =
- u_event_status.payload_size;
- err = msm_cpe_lsm_ioctl_shared(substream,
- cmd, event_status);
- }
- if (!err && copy_to_user(arg, event_status, u_pld_size)) {
- dev_err(rtd->dev,
- "%s: copy to user failed\n",
- __func__);
- kfree(event_status);
- err = -EFAULT;
- goto done;
- }
- msm_cpe_lsm_lab_start(substream, event_status->status);
- msm_cpe_process_event_status_done(lsm_d);
- kfree(event_status);
- }
- break;
- case SNDRV_LSM_SET_PARAMS: {
- struct snd_lsm_detection_params det_params;
- if (session->is_topology_used) {
- dev_err(rtd->dev,
- "%s: %s: not supported if using topology\n",
- __func__, "SNDRV_LSM_SET_PARAMS");
- err = -EINVAL;
- goto done;
- }
- if (copy_from_user(&det_params, (void *) arg,
- sizeof(det_params))) {
- dev_err(rtd->dev,
- "%s: %s: copy_from_user failed, size = %zd\n",
- __func__, "SNDRV_LSM_SET_PARAMS",
- sizeof(det_params));
- err = -EFAULT;
- goto done;
- }
- err = msm_cpe_lsm_ioctl_shared(substream, cmd,
- &det_params);
- }
- break;
- case SNDRV_LSM_SET_MODULE_PARAMS: {
- struct snd_lsm_module_params p_data;
- size_t p_size;
- u8 *params;
- if (!session->is_topology_used) {
- dev_err(rtd->dev,
- "%s: %s: not supported if not using topology\n",
- __func__, "SET_MODULE_PARAMS");
- err = -EINVAL;
- goto done;
- }
- if (!arg) {
- dev_err(rtd->dev,
- "%s: %s: No Param data to set\n",
- __func__, "SET_MODULE_PARAMS");
- err = -EINVAL;
- goto done;
- }
- if (copy_from_user(&p_data, arg,
- sizeof(p_data))) {
- dev_err(rtd->dev,
- "%s: %s: copy_from_user failed, size = %zd\n",
- __func__, "p_data", sizeof(p_data));
- err = -EFAULT;
- goto done;
- }
- if (p_data.num_params > LSM_PARAMS_MAX) {
- dev_err(rtd->dev,
- "%s: %s: Invalid num_params %d\n",
- __func__, "SET_MODULE_PARAMS",
- p_data.num_params);
- err = -EINVAL;
- goto done;
- }
- p_size = p_data.num_params *
- sizeof(struct lsm_params_info);
- if (p_data.data_size != p_size) {
- dev_err(rtd->dev,
- "%s: %s: Invalid size %zd\n",
- __func__, "SET_MODULE_PARAMS", p_size);
- err = -EFAULT;
- goto done;
- }
- params = kzalloc(p_size, GFP_KERNEL);
- if (!params) {
- err = -ENOMEM;
- goto done;
- }
- if (copy_from_user(params, p_data.params,
- p_data.data_size)) {
- dev_err(rtd->dev,
- "%s: %s: copy_from_user failed, size = %d\n",
- __func__, "params", p_data.data_size);
- kfree(params);
- err = -EFAULT;
- goto done;
- }
- err = msm_cpe_lsm_process_params(substream, &p_data, params);
- if (err)
- dev_err(rtd->dev,
- "%s: %s: Failed to set params, err = %d\n",
- __func__, "SET_MODULE_PARAMS", err);
- kfree(params);
- break;
- }
- default:
- err = msm_cpe_lsm_ioctl_shared(substream, cmd, arg);
- break;
- }
- done:
- MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
- "lsm_api_lock");
- return err;
- }
- #ifdef CONFIG_COMPAT
- struct snd_lsm_sound_model_v2_32 {
- compat_uptr_t data;
- compat_uptr_t confidence_level;
- u32 data_size;
- enum lsm_detection_mode detection_mode;
- u8 num_confidence_levels;
- bool detect_failure;
- };
- struct snd_lsm_detection_params_32 {
- compat_uptr_t conf_level;
- enum lsm_detection_mode detect_mode;
- u8 num_confidence_levels;
- bool detect_failure;
- };
- struct lsm_params_info_32 {
- u32 module_id;
- u32 param_id;
- u32 param_size;
- compat_uptr_t param_data;
- uint32_t param_type;
- };
- struct snd_lsm_module_params_32 {
- compat_uptr_t params;
- u32 num_params;
- u32 data_size;
- };
- enum {
- SNDRV_LSM_REG_SND_MODEL_V2_32 =
- _IOW('U', 0x07, struct snd_lsm_sound_model_v2_32),
- SNDRV_LSM_SET_PARAMS32 =
- _IOW('U', 0x0A, struct snd_lsm_detection_params_32),
- SNDRV_LSM_SET_MODULE_PARAMS_32 =
- _IOW('U', 0x0B, struct snd_lsm_module_params_32),
- };
- static int msm_cpe_lsm_ioctl_compat(struct snd_pcm_substream *substream,
- unsigned int cmd, void *arg)
- {
- int err = 0;
- struct snd_soc_pcm_runtime *rtd;
- struct cpe_priv *cpe = NULL;
- struct cpe_lsm_data *lsm_d = NULL;
- struct cpe_lsm_session *session = NULL;
- struct wcd_cpe_lsm_ops *lsm_ops;
- if (!substream || !substream->private_data) {
- pr_err("%s: invalid substream (%pK)\n",
- __func__, substream);
- return -EINVAL;
- }
- rtd = substream->private_data;
- lsm_d = cpe_get_lsm_data(substream);
- cpe = cpe_get_private_data(substream);
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- __func__);
- return -EINVAL;
- }
- MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
- "lsm_api_lock");
- session = lsm_d->lsm_session;
- lsm_ops = &cpe->lsm_ops;
- switch (cmd) {
- case SNDRV_LSM_REG_SND_MODEL_V2_32: {
- struct snd_lsm_sound_model_v2 snd_model;
- struct snd_lsm_sound_model_v2_32 snd_model32;
- if (session->is_topology_used) {
- dev_err(rtd->dev,
- "%s: %s: not supported if using topology\n",
- __func__, "LSM_REG_SND_MODEL_V2_32");
- err = -EINVAL;
- goto done;
- }
- dev_dbg(rtd->dev,
- "%s: ioctl %s\n", __func__,
- "SNDRV_LSM_REG_SND_MODEL_V2_32");
- if (copy_from_user(&snd_model32, (void *)arg,
- sizeof(snd_model32))) {
- dev_err(rtd->dev,
- "%s: copy from user failed, size %zd\n",
- __func__,
- sizeof(snd_model32));
- err = -EFAULT;
- goto done;
- }
- snd_model.data = compat_ptr(snd_model32.data);
- snd_model.confidence_level =
- compat_ptr(snd_model32.confidence_level);
- snd_model.data_size = snd_model32.data_size;
- snd_model.detect_failure = snd_model32.detect_failure;
- snd_model.num_confidence_levels =
- snd_model32.num_confidence_levels;
- snd_model.detection_mode = snd_model32.detection_mode;
- cmd = SNDRV_LSM_REG_SND_MODEL_V2;
- err = msm_cpe_lsm_ioctl_shared(substream, cmd, &snd_model);
- if (err)
- dev_err(rtd->dev,
- "%s: %s failed, error = %d\n",
- __func__,
- "SNDRV_LSM_REG_SND_MODEL_V2_32",
- err);
- }
- break;
- case SNDRV_LSM_EVENT_STATUS: {
- struct snd_lsm_event_status *event_status = NULL;
- struct snd_lsm_event_status u_event_status32;
- struct snd_lsm_event_status *udata_32 = NULL;
- int u_pld_size = 0;
- dev_dbg(rtd->dev,
- "%s: ioctl %s\n", __func__,
- "SNDRV_LSM_EVENT_STATUS32");
- if (copy_from_user(&u_event_status32, (void *)arg,
- sizeof(struct snd_lsm_event_status))) {
- dev_err(rtd->dev,
- "%s: event status copy from user failed, size %zd\n",
- __func__,
- sizeof(struct snd_lsm_event_status));
- err = -EFAULT;
- goto done;
- }
- if (u_event_status32.payload_size >
- LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
- dev_err(rtd->dev,
- "%s: payload_size %d is invalid, max allowed = %d\n",
- __func__, u_event_status32.payload_size,
- LISTEN_MAX_STATUS_PAYLOAD_SIZE);
- err = -EINVAL;
- goto done;
- }
- u_pld_size = sizeof(struct snd_lsm_event_status) +
- u_event_status32.payload_size;
- event_status = kzalloc(u_pld_size, GFP_KERNEL);
- if (!event_status) {
- dev_err(rtd->dev,
- "%s: No memory for event status\n",
- __func__);
- err = -ENOMEM;
- goto done;
- } else {
- event_status->payload_size =
- u_event_status32.payload_size;
- err = msm_cpe_lsm_ioctl_shared(substream,
- cmd, event_status);
- if (err)
- dev_err(rtd->dev,
- "%s: %s failed, error = %d\n",
- __func__,
- "SNDRV_LSM_EVENT_STATUS32",
- err);
- }
- if (!err) {
- udata_32 = kzalloc(u_pld_size, GFP_KERNEL);
- if (!udata_32) {
- dev_err(rtd->dev,
- "%s: nomem for udata\n",
- __func__);
- err = -EFAULT;
- } else {
- udata_32->status = event_status->status;
- udata_32->payload_size =
- event_status->payload_size;
- memcpy(udata_32->payload,
- event_status->payload,
- u_pld_size);
- }
- }
- if (!err && copy_to_user(arg, udata_32,
- u_pld_size)) {
- dev_err(rtd->dev,
- "%s: copy to user failed\n",
- __func__);
- kfree(event_status);
- kfree(udata_32);
- err = -EFAULT;
- goto done;
- }
- msm_cpe_lsm_lab_start(substream, event_status->status);
- msm_cpe_process_event_status_done(lsm_d);
- kfree(event_status);
- kfree(udata_32);
- }
- break;
- case SNDRV_LSM_EVENT_STATUS_V3: {
- struct snd_lsm_event_status_v3 *event_status = NULL;
- struct snd_lsm_event_status_v3 u_event_status32;
- struct snd_lsm_event_status_v3 *udata_32 = NULL;
- int u_pld_size = 0;
- dev_dbg(rtd->dev,
- "%s: ioctl %s\n", __func__,
- "SNDRV_LSM_EVENT_STATUS_V3_32");
- if (copy_from_user(&u_event_status32, (void *)arg,
- sizeof(struct snd_lsm_event_status_v3))) {
- dev_err(rtd->dev,
- "%s: event status copy from user failed, size %zd\n",
- __func__,
- sizeof(struct snd_lsm_event_status_v3));
- err = -EFAULT;
- goto done;
- }
- if (u_event_status32.payload_size >
- LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
- dev_err(rtd->dev,
- "%s: payload_size %d is invalid, max allowed = %d\n",
- __func__, u_event_status32.payload_size,
- LISTEN_MAX_STATUS_PAYLOAD_SIZE);
- err = -EINVAL;
- goto done;
- }
- u_pld_size = sizeof(struct snd_lsm_event_status_v3) +
- u_event_status32.payload_size;
- event_status = kzalloc(u_pld_size, GFP_KERNEL);
- if (!event_status) {
- dev_err(rtd->dev,
- "%s: No memory for event status\n",
- __func__);
- err = -ENOMEM;
- goto done;
- } else {
- event_status->payload_size =
- u_event_status32.payload_size;
- err = msm_cpe_lsm_ioctl_shared(substream,
- cmd, event_status);
- if (err)
- dev_err(rtd->dev,
- "%s: %s failed, error = %d\n",
- __func__,
- "SNDRV_LSM_EVENT_STATUS_V3_32",
- err);
- }
- if (!err) {
- udata_32 = kzalloc(u_pld_size, GFP_KERNEL);
- if (!udata_32) {
- dev_err(rtd->dev,
- "%s: nomem for udata\n",
- __func__);
- err = -EFAULT;
- } else {
- udata_32->timestamp_lsw =
- event_status->timestamp_lsw;
- udata_32->timestamp_msw =
- event_status->timestamp_msw;
- udata_32->status = event_status->status;
- udata_32->payload_size =
- event_status->payload_size;
- memcpy(udata_32->payload,
- event_status->payload,
- u_pld_size);
- }
- }
- if (!err && copy_to_user(arg, udata_32,
- u_pld_size)) {
- dev_err(rtd->dev,
- "%s: copy to user failed\n",
- __func__);
- kfree(event_status);
- kfree(udata_32);
- err = -EFAULT;
- goto done;
- }
- msm_cpe_lsm_lab_start(substream, event_status->status);
- msm_cpe_process_event_status_done(lsm_d);
- kfree(event_status);
- kfree(udata_32);
- }
- break;
- case SNDRV_LSM_SET_PARAMS32: {
- struct snd_lsm_detection_params_32 det_params32;
- struct snd_lsm_detection_params det_params;
- if (session->is_topology_used) {
- dev_err(rtd->dev,
- "%s: %s: not supported if using topology\n",
- __func__, "SNDRV_LSM_SET_PARAMS32");
- err = -EINVAL;
- goto done;
- }
- if (copy_from_user(&det_params32, arg,
- sizeof(det_params32))) {
- err = -EFAULT;
- dev_err(rtd->dev,
- "%s: %s: copy_from_user failed, size = %zd\n",
- __func__, "SNDRV_LSM_SET_PARAMS_32",
- sizeof(det_params32));
- } else {
- det_params.conf_level =
- compat_ptr(det_params32.conf_level);
- det_params.detect_mode =
- det_params32.detect_mode;
- det_params.num_confidence_levels =
- det_params32.num_confidence_levels;
- det_params.detect_failure =
- det_params32.detect_failure;
- cmd = SNDRV_LSM_SET_PARAMS;
- err = msm_cpe_lsm_ioctl_shared(substream, cmd,
- &det_params);
- if (err)
- dev_err(rtd->dev,
- "%s: ioctl %s failed\n", __func__,
- "SNDRV_LSM_SET_PARAMS");
- }
- break;
- }
- case SNDRV_LSM_SET_MODULE_PARAMS_32: {
- struct snd_lsm_module_params_32 p_data_32;
- struct snd_lsm_module_params p_data;
- u8 *params, *params32;
- size_t p_size;
- struct lsm_params_info_32 *p_info_32;
- struct lsm_params_info *p_info;
- int i;
- if (!session->is_topology_used) {
- dev_err(rtd->dev,
- "%s: %s: not supported if not using topology\n",
- __func__, "SET_MODULE_PARAMS_32");
- err = -EINVAL;
- goto done;
- }
- if (copy_from_user(&p_data_32, arg,
- sizeof(p_data_32))) {
- dev_err(rtd->dev,
- "%s: %s: copy_from_user failed, size = %zd\n",
- __func__, "SET_MODULE_PARAMS_32",
- sizeof(p_data_32));
- err = -EFAULT;
- goto done;
- }
- p_data.params = compat_ptr(p_data_32.params);
- p_data.num_params = p_data_32.num_params;
- p_data.data_size = p_data_32.data_size;
- if (p_data.num_params > LSM_PARAMS_MAX) {
- dev_err(rtd->dev,
- "%s: %s: Invalid num_params %d\n",
- __func__, "SET_MODULE_PARAMS_32",
- p_data.num_params);
- err = -EINVAL;
- goto done;
- }
- if (p_data.data_size !=
- (p_data.num_params * sizeof(struct lsm_params_info_32))) {
- dev_err(rtd->dev,
- "%s: %s: Invalid size %d\n",
- __func__, "SET_MODULE_PARAMS_32",
- p_data.data_size);
- err = -EINVAL;
- goto done;
- }
- p_size = sizeof(struct lsm_params_info_32) *
- p_data.num_params;
- params32 = kzalloc(p_size, GFP_KERNEL);
- if (!params32) {
- err = -ENOMEM;
- goto done;
- }
- p_size = sizeof(struct lsm_params_info) * p_data.num_params;
- params = kzalloc(p_size, GFP_KERNEL);
- if (!params) {
- kfree(params32);
- err = -ENOMEM;
- goto done;
- }
- if (copy_from_user(params32, p_data.params,
- p_data.data_size)) {
- dev_err(rtd->dev,
- "%s: %s: copy_from_user failed, size = %d\n",
- __func__, "params32", p_data.data_size);
- kfree(params32);
- kfree(params);
- err = -EFAULT;
- goto done;
- }
- p_info_32 = (struct lsm_params_info_32 *) params32;
- p_info = (struct lsm_params_info *) params;
- for (i = 0; i < p_data.num_params; i++) {
- p_info->module_id = p_info_32->module_id;
- p_info->param_id = p_info_32->param_id;
- p_info->param_size = p_info_32->param_size;
- p_info->param_data = compat_ptr(p_info_32->param_data);
- p_info->param_type = p_info_32->param_type;
- p_info_32++;
- p_info++;
- }
- err = msm_cpe_lsm_process_params(substream,
- &p_data, params);
- if (err)
- dev_err(rtd->dev,
- "%s: Failed to process params, err = %d\n",
- __func__, err);
- kfree(params);
- kfree(params32);
- break;
- }
- case SNDRV_LSM_REG_SND_MODEL_V2:
- case SNDRV_LSM_SET_PARAMS:
- case SNDRV_LSM_SET_MODULE_PARAMS:
- /*
- * In ideal cases, the compat_ioctl should never be called
- * with the above unlocked ioctl commands. Print error
- * and return error if it does.
- */
- dev_err(rtd->dev,
- "%s: Invalid cmd for compat_ioctl\n",
- __func__);
- err = -EINVAL;
- break;
- default:
- err = msm_cpe_lsm_ioctl_shared(substream, cmd, arg);
- break;
- }
- done:
- MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
- "lsm_api_lock");
- return err;
- }
- #else
- #define msm_cpe_lsm_ioctl_compat NULL
- #endif
- /*
- * msm_cpe_lsm_prepare: prepare call from ASoC core for this platform
- * @substream: ASoC substream for which the operation is invoked
- *
- * start the AFE port on CPE associated for this listen session
- */
- static int msm_cpe_lsm_prepare(struct snd_pcm_substream *substream)
- {
- int rc = 0;
- struct cpe_priv *cpe = cpe_get_private_data(substream);
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct wcd_cpe_afe_ops *afe_ops;
- struct wcd_cpe_afe_port_cfg *afe_cfg;
- struct cpe_lsm_session *lsm_session;
- struct cpe_lsm_lab *lab_d = &lsm_d->lab;
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct lsm_hw_params lsm_param;
- struct wcd_cpe_lsm_ops *lsm_ops;
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- __func__);
- return -EINVAL;
- }
- if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
- runtime->status->state == SNDRV_PCM_STATE_PREPARED) {
- pr_err("%s: XRUN ignore for now\n", __func__);
- return 0;
- }
- lsm_session = lsm_d->lsm_session;
- lab_d->pcm_size = snd_pcm_lib_buffer_bytes(substream);
- dev_dbg(rtd->dev,
- "%s: pcm_size 0x%x", __func__, lab_d->pcm_size);
- if (lsm_d->cpe_prepared) {
- dev_dbg(rtd->dev, "%s: CPE is alredy prepared\n",
- __func__);
- return 0;
- }
- lsm_ops = &cpe->lsm_ops;
- afe_ops = &cpe->afe_ops;
- afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
- switch (cpe->input_port_id) {
- case AFE_PORT_ID_3:
- afe_cfg->port_id = AFE_PORT_ID_3;
- afe_cfg->bit_width = 16;
- afe_cfg->num_channels = 1;
- afe_cfg->sample_rate = SAMPLE_RATE_48KHZ;
- rc = afe_ops->afe_port_cmd_cfg(cpe->core_handle, afe_cfg);
- break;
- case AFE_PORT_ID_1:
- default:
- afe_cfg->port_id = AFE_PORT_ID_1;
- afe_cfg->bit_width = 16;
- afe_cfg->num_channels = 1;
- afe_cfg->sample_rate = SAMPLE_RATE_16KHZ;
- rc = afe_ops->afe_set_params(cpe->core_handle,
- afe_cfg, cpe->afe_mad_ctl);
- break;
- }
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: cpe afe params failed for port = %d, err = %d\n",
- __func__, afe_cfg->port_id, rc);
- return rc;
- }
- lsm_param.sample_rate = afe_cfg->sample_rate;
- lsm_param.num_chs = afe_cfg->num_channels;
- lsm_param.bit_width = afe_cfg->bit_width;
- rc = lsm_ops->lsm_set_media_fmt_params(cpe->core_handle, lsm_session,
- &lsm_param);
- if (rc)
- dev_dbg(rtd->dev,
- "%s: failed to set lsm media fmt params, err = %d\n",
- __func__, rc);
- /* Send connect to port (input) */
- rc = lsm_ops->lsm_set_port(cpe->core_handle, lsm_session,
- &cpe->input_port_id);
- if (rc) {
- dev_err(rtd->dev,
- "%s: Failed to set connect input port, err=%d\n",
- __func__, rc);
- return rc;
- }
- if (cpe->input_port_id != 3) {
- rc = lsm_ops->lsm_get_afe_out_port_id(cpe->core_handle,
- lsm_session);
- if (rc != 0) {
- dev_err(rtd->dev,
- "%s: failed to get port id, err = %d\n",
- __func__, rc);
- return rc;
- }
- /* Send connect to port (output) */
- rc = lsm_ops->lsm_set_port(cpe->core_handle, lsm_session,
- &lsm_session->afe_out_port_id);
- if (rc) {
- dev_err(rtd->dev,
- "%s: Failed to set connect output port, err=%d\n",
- __func__, rc);
- return rc;
- }
- }
- rc = msm_cpe_afe_port_cntl(substream,
- cpe->core_handle,
- afe_ops, afe_cfg,
- AFE_CMD_PORT_START);
- if (rc)
- dev_err(rtd->dev,
- "%s: cpe_afe_port start failed, err = %d\n",
- __func__, rc);
- else
- lsm_d->cpe_prepared = true;
- return rc;
- }
- /*
- * msm_cpe_lsm_trigger: trigger call from ASoC core for this platform
- * @substream: ASoC substream for which the operation is invoked
- * @cmd: the trigger command from framework
- *
- * suspend/resume the AFE port on CPE associated with listen session
- */
- static int msm_cpe_lsm_trigger(struct snd_pcm_substream *substream,
- int cmd)
- {
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_priv *cpe = cpe_get_private_data(substream);
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct wcd_cpe_afe_ops *afe_ops;
- struct wcd_cpe_afe_port_cfg *afe_cfg;
- int afe_cmd = AFE_CMD_INVALID;
- int rc = 0;
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid private data\n",
- __func__);
- return -EINVAL;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid session data\n",
- __func__);
- return -EINVAL;
- }
- afe_ops = &cpe->afe_ops;
- afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
- switch (cmd) {
- case SNDRV_PCM_TRIGGER_SUSPEND:
- case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
- afe_cmd = AFE_CMD_PORT_SUSPEND;
- break;
- case SNDRV_PCM_TRIGGER_RESUME:
- case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
- afe_cmd = AFE_CMD_PORT_RESUME;
- break;
- default:
- afe_cmd = AFE_CMD_INVALID;
- dev_dbg(rtd->dev,
- "%s: unhandled trigger cmd %d\n",
- __func__, cmd);
- break;
- }
- if (afe_cmd != AFE_CMD_INVALID)
- rc = msm_cpe_afe_port_cntl(substream,
- cpe->core_handle,
- afe_ops, afe_cfg,
- afe_cmd);
- return rc;
- }
- static int msm_cpe_lsm_hwparams(struct snd_pcm_substream *substream,
- struct snd_pcm_hw_params *params)
- {
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct cpe_priv *cpe = cpe_get_private_data(substream);
- struct cpe_lsm_session *session = NULL;
- struct cpe_hw_params *hw_params = NULL;
- if (!cpe || !cpe->core_handle) {
- dev_err(rtd->dev,
- "%s: Invalid %s\n",
- __func__,
- (!cpe) ? "cpe" : "core");
- return -EINVAL;
- }
- if (!lsm_d || !lsm_d->lsm_session) {
- dev_err(rtd->dev,
- "%s: Invalid %s\n",
- __func__,
- (!lsm_d) ? "priv_data" : "session");
- return -EINVAL;
- }
- session = lsm_d->lsm_session;
- hw_params = &lsm_d->hw_params;
- hw_params->buf_sz = (params_buffer_bytes(params)
- / params_periods(params));
- hw_params->period_count = params_periods(params);
- hw_params->channels = params_channels(params);
- hw_params->sample_rate = params_rate(params);
- if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE)
- hw_params->sample_size = 16;
- else if (params_format(params) ==
- SNDRV_PCM_FORMAT_S24_LE)
- hw_params->sample_size = 24;
- else if (params_format(params) ==
- SNDRV_PCM_FORMAT_S32_LE)
- hw_params->sample_size = 32;
- else {
- dev_err(rtd->dev,
- "%s: Invalid Format 0x%x\n",
- __func__, params_format(params));
- return -EINVAL;
- }
- dev_dbg(rtd->dev,
- "%s: Format %d buffer size(bytes) %d period count %d\n"
- " Channel %d period in bytes 0x%x Period Size 0x%x rate = %d\n",
- __func__, params_format(params), params_buffer_bytes(params),
- params_periods(params), params_channels(params),
- params_period_bytes(params), params_period_size(params),
- params_rate(params));
- return 0;
- }
- static snd_pcm_uframes_t msm_cpe_lsm_pointer(
- struct snd_pcm_substream *substream)
- {
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct cpe_lsm_session *session;
- struct cpe_lsm_lab *lab_d = &lsm_d->lab;
- session = lsm_d->lsm_session;
- if (lab_d->dma_write >= lab_d->pcm_size)
- lab_d->dma_write = 0;
- dev_dbg(rtd->dev,
- "%s:pcm_dma_pos = %d\n",
- __func__, lab_d->dma_write);
- return bytes_to_frames(runtime, (lab_d->dma_write));
- }
- static int msm_cpe_lsm_copy(struct snd_pcm_substream *substream, int a,
- unsigned long hwoff, void __user *buf, unsigned long fbytes)
- {
- struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
- struct snd_soc_pcm_runtime *rtd = substream->private_data;
- struct snd_pcm_runtime *runtime = substream->runtime;
- struct cpe_lsm_session *session;
- struct cpe_lsm_lab *lab_d = &lsm_d->lab;
- char *pcm_buf;
- int rc = 0;
- if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
- runtime->status->state == SNDRV_PCM_STATE_PREPARED) {
- pr_err("%s: XRUN ignore for now\n", __func__);
- return 0;
- }
- session = lsm_d->lsm_session;
- /* Check if buffer reading is already in error state */
- if (lab_d->thread_status == MSM_LSM_LAB_THREAD_ERROR) {
- dev_err(rtd->dev,
- "%s: Bufferring is in error state\n",
- __func__);
- /*
- * Advance the period so there is no wait in case
- * read is invoked even after error is propogated
- */
- atomic_inc(&lab_d->in_count);
- lab_d->dma_write += snd_pcm_lib_period_bytes(substream);
- snd_pcm_period_elapsed(substream);
- return -ENETRESET;
- } else if (lab_d->thread_status == MSM_LSM_LAB_THREAD_STOP) {
- dev_err(rtd->dev,
- "%s: Buferring is in stopped\n",
- __func__);
- return -EIO;
- }
- rc = wait_event_timeout(lab_d->period_wait,
- (atomic_read(&lab_d->in_count) ||
- atomic_read(&lab_d->abort_read)),
- (2 * HZ));
- if (atomic_read(&lab_d->abort_read)) {
- pr_debug("%s: LSM LAB Abort read\n", __func__);
- return -EIO;
- }
- if (lab_d->thread_status != MSM_LSM_LAB_THREAD_RUNNING) {
- pr_err("%s: Lab stopped\n", __func__);
- return -EIO;
- }
- if (!rc) {
- pr_err("%s:LAB err wait_event_timeout\n", __func__);
- rc = -EAGAIN;
- goto fail;
- }
- if (lab_d->buf_idx >= (lsm_d->hw_params.period_count))
- lab_d->buf_idx = 0;
- pcm_buf = (lab_d->pcm_buf[lab_d->buf_idx].mem);
- pr_debug("%s: Buf IDX = 0x%x pcm_buf %pK\n",
- __func__, lab_d->buf_idx, pcm_buf);
- if (pcm_buf) {
- if (copy_to_user(buf, pcm_buf, fbytes)) {
- pr_err("Failed to copy buf to user\n");
- rc = -EFAULT;
- goto fail;
- }
- }
- lab_d->buf_idx++;
- atomic_dec(&lab_d->in_count);
- return 0;
- fail:
- return rc;
- }
- /*
- * msm_asoc_cpe_lsm_probe: ASoC framework for lsm component driver
- * @component: component registered with ASoC core
- *
- * Allocate the private data for this component and obtain the ops for
- * lsm and afe modules from underlying driver. Also find the codec
- * for this component as specified by machine driver for ASoC framework.
- */
- static int msm_asoc_cpe_lsm_probe(struct snd_soc_component *component)
- {
- struct snd_soc_card *card;
- struct snd_soc_pcm_runtime *rtd;
- struct snd_soc_codec *codec;
- struct cpe_priv *cpe_priv;
- struct snd_soc_component *component_rtd = NULL;
- const struct snd_kcontrol_new *kcontrol;
- bool found_runtime = false;
- const char *cpe_dev_id = "qcom,msm-cpe-lsm-id";
- u32 port_id = 0;
- int ret = 0;
- if (!component || !component->card) {
- pr_err("%s: Invalid component or card\n",
- __func__);
- return -EINVAL;
- }
- card = component->card;
- /* Match component to codec */
- list_for_each_entry(rtd, &card->rtd_list, list) {
- component_rtd = snd_soc_rtdcom_lookup(rtd, DRV_NAME);
- if (!component_rtd)
- continue;
- if (!strcmp(component_rtd->name,
- component->name)) {
- found_runtime = true;
- break;
- }
- }
- if (!found_runtime) {
- dev_err(component->dev,
- "%s: Failed to find runtime for component\n",
- __func__);
- return -EINVAL;
- }
- ret = of_property_read_u32(component->dev->of_node, cpe_dev_id,
- &port_id);
- if (ret) {
- dev_dbg(component->dev,
- "%s: missing 0x%x in dt node\n", __func__, port_id);
- port_id = 1;
- }
- codec = rtd->codec;
- cpe_priv = kzalloc(sizeof(struct cpe_priv),
- GFP_KERNEL);
- if (!cpe_priv)
- return -ENOMEM;
- cpe_priv->codec = codec;
- cpe_priv->input_port_id = port_id;
- wcd_cpe_get_lsm_ops(&cpe_priv->lsm_ops);
- wcd_cpe_get_afe_ops(&cpe_priv->afe_ops);
- snd_soc_component_set_drvdata(component, cpe_priv);
- kcontrol = &msm_cpe_kcontrols[0];
- snd_ctl_add(card->snd_card, snd_ctl_new1(kcontrol, cpe_priv));
- return 0;
- }
- static const struct snd_pcm_ops msm_cpe_lsm_ops = {
- .open = msm_cpe_lsm_open,
- .close = msm_cpe_lsm_close,
- .ioctl = msm_cpe_lsm_ioctl,
- .prepare = msm_cpe_lsm_prepare,
- .trigger = msm_cpe_lsm_trigger,
- .pointer = msm_cpe_lsm_pointer,
- .copy_user = msm_cpe_lsm_copy,
- .hw_params = msm_cpe_lsm_hwparams,
- .compat_ioctl = msm_cpe_lsm_ioctl_compat,
- };
- static struct snd_soc_component_driver msm_soc_cpe_component = {
- .name = DRV_NAME,
- .ops = &msm_cpe_lsm_ops,
- .probe = msm_asoc_cpe_lsm_probe,
- };
- /*
- * msm_cpe_lsm_probe: platform driver probe
- * @pdev: platform device
- *
- * Register the ASoC platform driver with ASoC core
- */
- static int msm_cpe_lsm_probe(struct platform_device *pdev)
- {
- return snd_soc_register_component(&pdev->dev,
- &msm_soc_cpe_component,
- NULL, 0);
- }
- /*
- * msm_cpe_lsm_remove: platform driver remove
- * @pdev: platform device
- *
- * Deregister the ASoC platform driver
- */
- static int msm_cpe_lsm_remove(struct platform_device *pdev)
- {
- snd_soc_unregister_commponent(&pdev->dev);
- return 0;
- }
- static const struct of_device_id msm_cpe_lsm_dt_match[] = {
- {.compatible = "qcom,msm-cpe-lsm" },
- { }
- };
- static struct platform_driver msm_cpe_lsm_driver = {
- .driver = {
- .name = "msm-cpe-lsm",
- .owner = THIS_MODULE,
- .of_match_table = of_match_ptr(msm_cpe_lsm_dt_match),
- },
- .probe = msm_cpe_lsm_probe,
- .remove = msm_cpe_lsm_remove,
- };
- int __init msm_cpe_lsm_init(void)
- {
- return platform_driver_register(&msm_cpe_lsm_driver);
- }
- void __exit msm_cpe_lsm_exit(void)
- {
- platform_driver_unregister(&msm_cpe_lsm_driver);
- }
- module_init(msm_cpe_lsm_init);
- module_exit(msm_cpe_lsm_exit);
- MODULE_DESCRIPTION("CPE LSM platform driver");
- MODULE_DEVICE_TABLE(of, msm_cpe_lsm_dt_match);
- MODULE_LICENSE("GPL v2");
|