msm-cpe-lsm.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/module.h>
  7. #include <linux/device.h>
  8. #include <linux/slab.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/kthread.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/of.h>
  13. #include <linux/delay.h>
  14. #include <linux/sched.h>
  15. #include <linux/freezer.h>
  16. #include <sound/soc.h>
  17. #include <sound/lsm_params.h>
  18. #include <sound/pcm_params.h>
  19. #include "msm-slim-dma.h"
  20. #include "codecs/cpe_core.h"
  21. #define SAMPLE_RATE_48KHZ 48000
  22. #define SAMPLE_RATE_16KHZ 16000
  23. #define LSM_VOICE_WAKEUP_APP_V2 2
  24. #define AFE_PORT_ID_1 1
  25. #define AFE_PORT_ID_3 3
  26. #define AFE_OUT_PORT_2 2
  27. #define LISTEN_MIN_NUM_PERIODS 2
  28. #define LISTEN_MAX_NUM_PERIODS 12
  29. #define LISTEN_MAX_PERIOD_SIZE 61440
  30. #define LISTEN_MIN_PERIOD_SIZE 320
  31. #define LISTEN_MAX_STATUS_PAYLOAD_SIZE 256
  32. #define MSM_CPE_MAX_CUSTOM_PARAM_SIZE 2048
  33. #define MSM_CPE_LAB_THREAD_TIMEOUT (3 * (HZ/10))
  34. #define MSM_CPE_LSM_GRAB_LOCK(lock, name) \
  35. { \
  36. pr_debug("%s: %s lock acquire\n", \
  37. __func__, name); \
  38. mutex_lock(lock); \
  39. }
  40. #define MSM_CPE_LSM_REL_LOCK(lock, name) \
  41. { \
  42. pr_debug("%s: %s lock release\n", \
  43. __func__, name); \
  44. mutex_unlock(lock); \
  45. }
  46. /* Conventional and unconventional sample rate supported */
  47. static unsigned int supported_sample_rates[] = {
  48. 8000, 16000, 48000, 192000, 384000
  49. };
  50. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  51. .count = ARRAY_SIZE(supported_sample_rates),
  52. .list = supported_sample_rates,
  53. .mask = 0,
  54. };
  55. static struct snd_pcm_hardware msm_pcm_hardware_listen = {
  56. .info = (SNDRV_PCM_INFO_BLOCK_TRANSFER |
  57. SNDRV_PCM_INFO_MMAP_VALID |
  58. SNDRV_PCM_INFO_INTERLEAVED |
  59. SNDRV_PCM_INFO_PAUSE |
  60. SNDRV_PCM_INFO_RESUME),
  61. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  62. SNDRV_PCM_FMTBIT_S24_LE |
  63. SNDRV_PCM_FMTBIT_S32_LE),
  64. .rates = (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000 |
  65. SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_384000),
  66. .rate_min = 16000,
  67. .rate_max = 384000,
  68. .channels_min = 1,
  69. .channels_max = 1,
  70. .buffer_bytes_max = LISTEN_MAX_NUM_PERIODS *
  71. LISTEN_MAX_PERIOD_SIZE,
  72. .period_bytes_min = LISTEN_MIN_PERIOD_SIZE,
  73. .period_bytes_max = LISTEN_MAX_PERIOD_SIZE,
  74. .periods_min = LISTEN_MIN_NUM_PERIODS,
  75. .periods_max = LISTEN_MAX_NUM_PERIODS,
  76. .fifo_size = 0,
  77. };
  78. enum {
  79. AFE_CMD_INVALID = 0,
  80. AFE_CMD_PORT_START,
  81. AFE_CMD_PORT_SUSPEND,
  82. AFE_CMD_PORT_RESUME,
  83. AFE_CMD_PORT_STOP,
  84. };
  85. enum cpe_lab_thread_status {
  86. MSM_LSM_LAB_THREAD_STOP,
  87. MSM_LSM_LAB_THREAD_RUNNING,
  88. MSM_LSM_LAB_THREAD_ERROR,
  89. };
  90. struct cpe_hw_params {
  91. u32 sample_rate;
  92. u16 sample_size;
  93. u32 buf_sz;
  94. u32 period_count;
  95. u16 channels;
  96. };
  97. struct cpe_data_pcm_buf {
  98. u8 *mem;
  99. phys_addr_t phys;
  100. };
  101. struct cpe_lsm_lab {
  102. atomic_t in_count;
  103. atomic_t abort_read;
  104. u32 dma_write;
  105. u32 buf_idx;
  106. u32 pcm_size;
  107. enum cpe_lab_thread_status thread_status;
  108. struct cpe_data_pcm_buf *pcm_buf;
  109. wait_queue_head_t period_wait;
  110. struct completion comp;
  111. struct completion thread_complete;
  112. };
  113. struct cpe_priv {
  114. void *core_handle;
  115. struct snd_soc_codec *codec;
  116. struct wcd_cpe_lsm_ops lsm_ops;
  117. struct wcd_cpe_afe_ops afe_ops;
  118. bool afe_mad_ctl;
  119. u32 input_port_id;
  120. };
  121. struct cpe_lsm_data {
  122. struct device *dev;
  123. struct cpe_lsm_session *lsm_session;
  124. struct mutex lsm_api_lock;
  125. struct cpe_lsm_lab lab;
  126. struct cpe_hw_params hw_params;
  127. struct snd_pcm_substream *substream;
  128. wait_queue_head_t event_wait;
  129. atomic_t event_avail;
  130. atomic_t event_stop;
  131. u8 ev_det_status;
  132. u8 ev_det_pld_size;
  133. u8 *ev_det_payload;
  134. bool cpe_prepared;
  135. };
  136. static int msm_cpe_afe_mad_ctl_get(struct snd_kcontrol *kcontrol,
  137. struct snd_ctl_elem_value *ucontrol)
  138. {
  139. struct cpe_priv *cpe = kcontrol->private_data;
  140. ucontrol->value.integer.value[0] = cpe->afe_mad_ctl;
  141. return 0;
  142. }
  143. static int msm_cpe_afe_mad_ctl_put(struct snd_kcontrol *kcontrol,
  144. struct snd_ctl_elem_value *ucontrol)
  145. {
  146. struct cpe_priv *cpe = kcontrol->private_data;
  147. cpe->afe_mad_ctl = ucontrol->value.integer.value[0];
  148. return 0;
  149. }
  150. static struct snd_kcontrol_new msm_cpe_kcontrols[] = {
  151. SOC_SINGLE_EXT("CPE AFE MAD Enable", SND_SOC_NOPM, 0, 1, 0,
  152. msm_cpe_afe_mad_ctl_get, msm_cpe_afe_mad_ctl_put),
  153. };
  154. /*
  155. * cpe_get_private_data: obtain ASoC platform driver private data
  156. * @substream: ASoC substream for which private data to be obtained
  157. */
  158. static struct cpe_priv *cpe_get_private_data(
  159. struct snd_pcm_substream *substream)
  160. {
  161. struct snd_soc_pcm_runtime *rtd;
  162. if (!substream || !substream->private_data) {
  163. pr_err("%s: %s is invalid\n",
  164. __func__,
  165. (!substream) ? "substream" : "private_data");
  166. goto err_ret;
  167. }
  168. rtd = substream->private_data;
  169. if (!rtd || !rtd->platform) {
  170. pr_err("%s: %s is invalid\n",
  171. __func__,
  172. (!rtd) ? "runtime" : "platform");
  173. goto err_ret;
  174. }
  175. return snd_soc_platform_get_drvdata(rtd->platform);
  176. err_ret:
  177. return NULL;
  178. }
  179. /*
  180. * cpe_get_lsm_data: obtain the lsm session data given the substream
  181. * @substream: ASoC substream for which lsm session data to be obtained
  182. */
  183. static struct cpe_lsm_data *cpe_get_lsm_data(
  184. struct snd_pcm_substream *substream)
  185. {
  186. struct snd_pcm_runtime *runtime = substream->runtime;
  187. return runtime->private_data;
  188. }
  189. static void msm_cpe_process_event_status(void *data,
  190. u8 detect_status, u8 size, u8 *payload)
  191. {
  192. struct cpe_lsm_data *lsm_d = data;
  193. lsm_d->ev_det_status = detect_status;
  194. lsm_d->ev_det_pld_size = size;
  195. lsm_d->ev_det_payload = kzalloc(size, GFP_KERNEL);
  196. if (!lsm_d->ev_det_payload)
  197. return;
  198. memcpy(lsm_d->ev_det_payload, payload, size);
  199. atomic_set(&lsm_d->event_avail, 1);
  200. wake_up(&lsm_d->event_wait);
  201. }
  202. static void msm_cpe_process_event_status_done(struct cpe_lsm_data *lsm_data)
  203. {
  204. kfree(lsm_data->ev_det_payload);
  205. lsm_data->ev_det_payload = NULL;
  206. lsm_data->ev_det_status = 0;
  207. lsm_data->ev_det_pld_size = 0;
  208. }
  209. /*
  210. * msm_cpe_afe_port_cntl: Perform the afe port control
  211. * @substream: substream for which afe port command to be performed
  212. * @core_handle: handle to core
  213. * @afe_ops: handle to the afe operations
  214. * @afe_cfg: afe port configuration data
  215. * @cmd: command to be sent to AFE
  216. *
  217. */
  218. static int msm_cpe_afe_port_cntl(
  219. struct snd_pcm_substream *substream,
  220. void *core_handle,
  221. struct wcd_cpe_afe_ops *afe_ops,
  222. struct wcd_cpe_afe_port_cfg *afe_cfg,
  223. int cmd)
  224. {
  225. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  226. int rc = 0;
  227. if (!afe_cfg->port_id) {
  228. /*
  229. * It is possible driver can get closed without prepare,
  230. * in which case afe ports will not be initialized.
  231. */
  232. dev_dbg(rtd->dev,
  233. "%s: Invalid afe port id\n",
  234. __func__);
  235. return 0;
  236. }
  237. switch (cmd) {
  238. case AFE_CMD_PORT_START:
  239. rc = afe_ops->afe_port_start(core_handle, afe_cfg);
  240. if (rc != 0)
  241. dev_err(rtd->dev,
  242. "%s: AFE port start failed\n",
  243. __func__);
  244. break;
  245. case AFE_CMD_PORT_SUSPEND:
  246. rc = afe_ops->afe_port_suspend(core_handle, afe_cfg);
  247. if (rc != 0)
  248. dev_err(rtd->dev,
  249. "%s: afe_suspend failed, err = %d\n",
  250. __func__, rc);
  251. break;
  252. case AFE_CMD_PORT_RESUME:
  253. rc = afe_ops->afe_port_resume(core_handle, afe_cfg);
  254. if (rc != 0)
  255. dev_err(rtd->dev,
  256. "%s: afe_resume failed, err = %d\n",
  257. __func__, rc);
  258. break;
  259. case AFE_CMD_PORT_STOP:
  260. rc = afe_ops->afe_port_stop(core_handle, afe_cfg);
  261. if (rc != 0)
  262. dev_err(rtd->dev,
  263. "%s: afe_stopfailed, err = %d\n",
  264. __func__, rc);
  265. break;
  266. }
  267. return rc;
  268. }
  269. static int msm_cpe_lsm_lab_stop(struct snd_pcm_substream *substream)
  270. {
  271. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  272. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  273. struct cpe_priv *cpe = cpe_get_private_data(substream);
  274. struct wcd_cpe_lsm_ops *lsm_ops;
  275. struct wcd_cpe_afe_ops *afe_ops;
  276. struct cpe_lsm_session *session;
  277. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  278. struct msm_slim_dma_data *dma_data = NULL;
  279. int rc;
  280. /*
  281. * the caller is not aware of LAB status and will
  282. * try to stop lab even if it is already stopped.
  283. * return success right away is LAB is already stopped
  284. */
  285. if (lab_d->thread_status == MSM_LSM_LAB_THREAD_STOP) {
  286. dev_dbg(rtd->dev,
  287. "%s: lab already stopped\n",
  288. __func__);
  289. return 0;
  290. }
  291. if (!cpe || !cpe->core_handle) {
  292. dev_err(rtd->dev,
  293. "%s: Invalid private data\n",
  294. __func__);
  295. return -EINVAL;
  296. }
  297. if (!lsm_d->lsm_session) {
  298. dev_err(rtd->dev,
  299. "%s: Invalid session data\n",
  300. __func__);
  301. return -EINVAL;
  302. }
  303. lsm_ops = &cpe->lsm_ops;
  304. afe_ops = &cpe->afe_ops;
  305. session = lsm_d->lsm_session;
  306. if (rtd->cpu_dai)
  307. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
  308. substream);
  309. if (!dma_data || !dma_data->dai_channel_ctl) {
  310. dev_err(rtd->dev,
  311. "%s: dma_data is not set\n",
  312. __func__);
  313. return -EINVAL;
  314. }
  315. if (lab_d->thread_status == MSM_LSM_LAB_THREAD_RUNNING) {
  316. dev_dbg(rtd->dev, "%s: stopping lab thread\n",
  317. __func__);
  318. rc = kthread_stop(session->lsm_lab_thread);
  319. /*
  320. * kthread_stop returns EINTR if the thread_fn
  321. * was not scheduled before calling kthread_stop.
  322. * In this case, we dont need to wait for lab
  323. * thread to complete as lab thread will not be
  324. * scheduled at all.
  325. */
  326. if (rc == -EINTR)
  327. goto done;
  328. /* Wait for the lab thread to exit */
  329. rc = wait_for_completion_timeout(
  330. &lab_d->thread_complete,
  331. MSM_CPE_LAB_THREAD_TIMEOUT);
  332. if (!rc) {
  333. dev_err(rtd->dev,
  334. "%s: Wait for lab thread timedout\n",
  335. __func__);
  336. return -ETIMEDOUT;
  337. }
  338. }
  339. rc = lsm_ops->lab_ch_setup(cpe->core_handle,
  340. session,
  341. WCD_CPE_PRE_DISABLE);
  342. if (rc)
  343. dev_err(rtd->dev,
  344. "%s: PRE ch teardown failed, err = %d\n",
  345. __func__, rc);
  346. /* continue with teardown even if any intermediate step fails */
  347. rc = dma_data->dai_channel_ctl(dma_data, rtd->cpu_dai, false);
  348. if (rc)
  349. dev_err(rtd->dev,
  350. "%s: open data failed %d\n", __func__, rc);
  351. dma_data->ph = 0;
  352. /*
  353. * Even though LAB stop failed,
  354. * output AFE port needs to be stopped
  355. */
  356. rc = afe_ops->afe_port_stop(cpe->core_handle,
  357. &session->afe_out_port_cfg);
  358. if (rc)
  359. dev_err(rtd->dev,
  360. "%s: AFE out port stop failed, err = %d\n",
  361. __func__, rc);
  362. rc = lsm_ops->lab_ch_setup(cpe->core_handle,
  363. session,
  364. WCD_CPE_POST_DISABLE);
  365. if (rc)
  366. dev_err(rtd->dev,
  367. "%s: POST ch teardown failed, err = %d\n",
  368. __func__, rc);
  369. done:
  370. lab_d->thread_status = MSM_LSM_LAB_THREAD_STOP;
  371. lab_d->buf_idx = 0;
  372. atomic_set(&lab_d->in_count, 0);
  373. lab_d->dma_write = 0;
  374. return 0;
  375. }
  376. static int msm_cpe_lab_buf_alloc(struct snd_pcm_substream *substream,
  377. struct cpe_lsm_session *session,
  378. struct msm_slim_dma_data *dma_data)
  379. {
  380. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  381. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  382. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  383. struct cpe_hw_params *hw_params = &lsm_d->hw_params;
  384. struct cpe_data_pcm_buf *pcm_buf = NULL;
  385. int rc = 0;
  386. int dma_alloc = 0;
  387. u32 count = 0;
  388. u32 bufsz, bufcnt;
  389. if (lab_d->pcm_buf &&
  390. lab_d->pcm_buf->mem) {
  391. dev_dbg(rtd->dev,
  392. "%s: LAB buf already allocated\n",
  393. __func__);
  394. goto exit;
  395. }
  396. bufsz = hw_params->buf_sz;
  397. bufcnt = hw_params->period_count;
  398. dev_dbg(rtd->dev,
  399. "%s:Buf Size %d Buf count %d\n",
  400. __func__,
  401. bufsz, bufcnt);
  402. pcm_buf = kzalloc(((sizeof(struct cpe_data_pcm_buf)) * bufcnt),
  403. GFP_KERNEL);
  404. if (!pcm_buf) {
  405. rc = -ENOMEM;
  406. goto exit;
  407. }
  408. lab_d->pcm_buf = pcm_buf;
  409. dma_alloc = bufsz * bufcnt;
  410. pcm_buf->mem = NULL;
  411. pcm_buf->mem = kzalloc(dma_alloc, GFP_DMA);
  412. if (!pcm_buf->mem) {
  413. rc = -ENOMEM;
  414. goto fail;
  415. }
  416. pcm_buf->phys = dma_map_single(dma_data->sdev->dev.parent,
  417. pcm_buf->mem, dma_alloc, DMA_BIDIRECTIONAL);
  418. if (dma_mapping_error(dma_data->sdev->dev.parent, pcm_buf->phys)) {
  419. dev_err(rtd->dev, "%s Error mapping DMA buffers\n", __func__);
  420. pcm_buf->phys = (phys_addr_t)NULL;
  421. rc = -EFAULT;
  422. goto fail;
  423. }
  424. count = 0;
  425. while (count < bufcnt) {
  426. pcm_buf[count].mem = pcm_buf[0].mem + (count * bufsz);
  427. pcm_buf[count].phys = pcm_buf[0].phys + (count * bufsz);
  428. dev_dbg(rtd->dev,
  429. "%s: pcm_buf[%d].mem %pK pcm_buf[%d].phys %pK\n",
  430. __func__, count,
  431. (void *)pcm_buf[count].mem,
  432. count, &(pcm_buf[count].phys));
  433. count++;
  434. }
  435. return 0;
  436. fail:
  437. if (pcm_buf && pcm_buf->mem)
  438. kfree(pcm_buf->mem);
  439. kfree(pcm_buf);
  440. lab_d->pcm_buf = NULL;
  441. exit:
  442. return rc;
  443. }
  444. static int msm_cpe_lab_buf_dealloc(struct snd_pcm_substream *substream,
  445. struct cpe_lsm_session *session, struct msm_slim_dma_data *dma_data)
  446. {
  447. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  448. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  449. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  450. struct cpe_hw_params *hw_params = &lsm_d->hw_params;
  451. int rc = 0;
  452. int dma_alloc = 0;
  453. struct cpe_data_pcm_buf *pcm_buf = NULL;
  454. int bufsz, bufcnt;
  455. bufsz = hw_params->buf_sz;
  456. bufcnt = hw_params->period_count;
  457. dev_dbg(rtd->dev,
  458. "%s:Buf Size %d Buf count %d\n", __func__,
  459. bufsz, bufcnt);
  460. if (bufcnt <= 0 || bufsz <= 0) {
  461. dev_err(rtd->dev,
  462. "%s: Invalid params, bufsz = %u, bufcnt = %u\n",
  463. __func__, bufsz, bufcnt);
  464. return -EINVAL;
  465. }
  466. pcm_buf = lab_d->pcm_buf;
  467. dma_alloc = bufsz * bufcnt;
  468. if (dma_data && pcm_buf)
  469. if (pcm_buf->phys)
  470. dma_unmap_single(dma_data->sdev->dev.parent,
  471. pcm_buf->phys, dma_alloc, DMA_BIDIRECTIONAL);
  472. if (pcm_buf)
  473. kfree(pcm_buf->mem);
  474. kfree(pcm_buf);
  475. lab_d->pcm_buf = NULL;
  476. return rc;
  477. }
  478. /*
  479. * msm_cpe_lab_thread: Initiated on KW detection
  480. * @data: lab data
  481. *
  482. * Start lab thread and call CPE core API for SLIM
  483. * read operations.
  484. */
  485. static int msm_cpe_lab_thread(void *data)
  486. {
  487. struct cpe_lsm_data *lsm_d = data;
  488. struct cpe_lsm_session *session = lsm_d->lsm_session;
  489. struct snd_pcm_substream *substream = lsm_d->substream;
  490. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  491. struct cpe_hw_params *hw_params = &lsm_d->hw_params;
  492. struct cpe_priv *cpe = cpe_get_private_data(substream);
  493. struct wcd_cpe_lsm_ops *lsm_ops;
  494. struct wcd_cpe_afe_ops *afe_ops;
  495. struct cpe_data_pcm_buf *cur_buf, *next_buf;
  496. struct msm_slim_dma_data *dma_data = NULL;
  497. struct snd_soc_pcm_runtime *rtd = NULL;
  498. bool wait_timedout = false;
  499. int rc = 0;
  500. u32 done_len = 0;
  501. u32 buf_count = 0;
  502. u32 prd_cnt;
  503. allow_signal(SIGKILL);
  504. set_current_state(TASK_INTERRUPTIBLE);
  505. pr_debug("%s: Lab thread start\n", __func__);
  506. init_completion(&lab_d->comp);
  507. if (PCM_RUNTIME_CHECK(substream)) {
  508. rc = -EINVAL;
  509. goto done;
  510. }
  511. if (!cpe || !cpe->core_handle) {
  512. pr_err("%s: Handle to %s is invalid\n",
  513. __func__,
  514. (!cpe) ? "cpe" : "core");
  515. rc = -EINVAL;
  516. goto done;
  517. }
  518. rtd = substream->private_data;
  519. if (rtd->cpu_dai)
  520. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
  521. substream);
  522. if (!dma_data || !dma_data->dai_channel_ctl) {
  523. pr_err("%s: dma_data is not set\n", __func__);
  524. rc = -EINVAL;
  525. goto done;
  526. }
  527. lsm_ops = &cpe->lsm_ops;
  528. afe_ops = &cpe->afe_ops;
  529. rc = lsm_ops->lab_ch_setup(cpe->core_handle,
  530. session,
  531. WCD_CPE_PRE_ENABLE);
  532. if (rc) {
  533. dev_err(rtd->dev,
  534. "%s: PRE ch setup failed, err = %d\n",
  535. __func__, rc);
  536. goto done;
  537. }
  538. rc = dma_data->dai_channel_ctl(dma_data, rtd->cpu_dai, true);
  539. if (rc) {
  540. dev_err(rtd->dev,
  541. "%s: open data failed %d\n", __func__, rc);
  542. goto done;
  543. }
  544. dev_dbg(rtd->dev, "%s: Established data channel\n",
  545. __func__);
  546. init_waitqueue_head(&lab_d->period_wait);
  547. memset(lab_d->pcm_buf[0].mem, 0, lab_d->pcm_size);
  548. rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
  549. lab_d->pcm_buf[0].mem,
  550. hw_params->buf_sz, &lab_d->comp);
  551. if (rc) {
  552. dev_err(rtd->dev,
  553. "%s: buf[0] slim_port_xfer failed, err = %d\n",
  554. __func__, rc);
  555. goto done;
  556. }
  557. rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
  558. lab_d->pcm_buf[1].mem,
  559. hw_params->buf_sz, &lab_d->comp);
  560. if (rc) {
  561. dev_err(rtd->dev,
  562. "%s: buf[0] slim_port_xfer failed, err = %d\n",
  563. __func__, rc);
  564. goto done;
  565. }
  566. cur_buf = &lab_d->pcm_buf[0];
  567. next_buf = &lab_d->pcm_buf[2];
  568. prd_cnt = hw_params->period_count;
  569. rc = lsm_ops->lab_ch_setup(cpe->core_handle,
  570. session,
  571. WCD_CPE_POST_ENABLE);
  572. if (rc) {
  573. dev_err(rtd->dev,
  574. "%s: POST ch setup failed, err = %d\n",
  575. __func__, rc);
  576. goto done;
  577. }
  578. rc = afe_ops->afe_port_start(cpe->core_handle,
  579. &session->afe_out_port_cfg);
  580. if (rc) {
  581. dev_err(rtd->dev,
  582. "%s: AFE out port start failed, err = %d\n",
  583. __func__, rc);
  584. goto done;
  585. }
  586. while (!kthread_should_stop() &&
  587. lab_d->thread_status != MSM_LSM_LAB_THREAD_ERROR) {
  588. rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
  589. next_buf->mem,
  590. hw_params->buf_sz, &lab_d->comp);
  591. if (rc) {
  592. dev_err(rtd->dev,
  593. "%s: slim_port_xfer failed, err = %d\n",
  594. __func__, rc);
  595. lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
  596. }
  597. rc = wait_for_completion_timeout(&lab_d->comp, (2 * HZ/10));
  598. if (!rc) {
  599. dev_err(rtd->dev,
  600. "%s: wait timedout for slim buffer\n",
  601. __func__);
  602. wait_timedout = true;
  603. } else {
  604. wait_timedout = false;
  605. }
  606. rc = slim_port_get_xfer_status(dma_data->sdev,
  607. dma_data->ph,
  608. &cur_buf->phys, &done_len);
  609. if (rc ||
  610. (!rc && wait_timedout)) {
  611. dev_err(rtd->dev,
  612. "%s: xfer_status failure, rc = %d, wait_timedout = %s\n",
  613. __func__, rc,
  614. (wait_timedout ? "true" : "false"));
  615. lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
  616. }
  617. if (done_len ||
  618. ((!done_len) &&
  619. lab_d->thread_status == MSM_LSM_LAB_THREAD_ERROR)) {
  620. atomic_inc(&lab_d->in_count);
  621. lab_d->dma_write += snd_pcm_lib_period_bytes(substream);
  622. snd_pcm_period_elapsed(substream);
  623. wake_up(&lab_d->period_wait);
  624. buf_count++;
  625. cur_buf = &lab_d->pcm_buf[buf_count % prd_cnt];
  626. next_buf = &lab_d->pcm_buf[(buf_count + 2) % prd_cnt];
  627. dev_dbg(rtd->dev,
  628. "%s: Cur buf.mem = %pK Next Buf.mem = %pK\n"
  629. " buf count = 0x%x\n", __func__,
  630. cur_buf->mem, next_buf->mem, buf_count);
  631. } else {
  632. dev_err(rtd->dev,
  633. "%s: SB get status, invalid len = 0x%x\n",
  634. __func__, done_len);
  635. }
  636. done_len = 0;
  637. }
  638. done:
  639. if (rc)
  640. lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
  641. pr_debug("%s: Exit lab_thread, exit_status=%d, thread_status=%d\n",
  642. __func__, rc, lab_d->thread_status);
  643. complete(&lab_d->thread_complete);
  644. return 0;
  645. }
  646. /*
  647. * msm_cpe_lsm_open: ASoC call to open the stream
  648. * @substream: substream that is to be opened
  649. *
  650. * Create session data for lsm session and open the lsm session
  651. * on CPE.
  652. */
  653. static int msm_cpe_lsm_open(struct snd_pcm_substream *substream)
  654. {
  655. struct cpe_lsm_data *lsm_d;
  656. struct snd_pcm_runtime *runtime = substream->runtime;
  657. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  658. struct cpe_priv *cpe = cpe_get_private_data(substream);
  659. struct wcd_cpe_lsm_ops *lsm_ops;
  660. int rc = 0;
  661. if (!cpe || !cpe->codec) {
  662. dev_err(rtd->dev,
  663. "%s: Invalid private data\n",
  664. __func__);
  665. return -EINVAL;
  666. }
  667. runtime->hw = msm_pcm_hardware_listen;
  668. rc = snd_pcm_hw_constraint_list(runtime, 0,
  669. SNDRV_PCM_HW_PARAM_RATE,
  670. &constraints_sample_rates);
  671. if (rc < 0) {
  672. pr_err("snd_pcm_hw_constraint_list failed rc %d\n", rc);
  673. return -EINVAL;
  674. }
  675. /* Ensure that buffer size is a multiple of period size */
  676. rc = snd_pcm_hw_constraint_integer(runtime,
  677. SNDRV_PCM_HW_PARAM_PERIODS);
  678. if (rc < 0) {
  679. pr_err("%s: Unable to set pcm_param_periods, rc %d\n",
  680. __func__, rc);
  681. return -EINVAL;
  682. }
  683. rc = snd_pcm_hw_constraint_minmax(runtime,
  684. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  685. LISTEN_MIN_NUM_PERIODS * LISTEN_MIN_PERIOD_SIZE,
  686. LISTEN_MAX_NUM_PERIODS * LISTEN_MAX_PERIOD_SIZE);
  687. if (rc < 0) {
  688. pr_err("%s: Unable to set pcm constraints, rc %d\n",
  689. __func__, rc);
  690. return -EINVAL;
  691. }
  692. cpe->core_handle = wcd_cpe_get_core_handle(cpe->codec);
  693. if (!cpe->core_handle) {
  694. dev_err(rtd->dev,
  695. "%s: Invalid handle to codec core\n",
  696. __func__);
  697. return -EINVAL;
  698. }
  699. lsm_ops = &cpe->lsm_ops;
  700. lsm_d = kzalloc(sizeof(struct cpe_lsm_data), GFP_KERNEL);
  701. if (!lsm_d) {
  702. dev_err(rtd->dev,
  703. "%s: ENOMEM for lsm session, size = %zd\n",
  704. __func__, sizeof(struct cpe_lsm_data));
  705. rc = -ENOMEM;
  706. goto fail_return;
  707. }
  708. mutex_init(&lsm_d->lsm_api_lock);
  709. lsm_d->lsm_session = lsm_ops->lsm_alloc_session(cpe->core_handle,
  710. lsm_d, msm_cpe_process_event_status);
  711. if (!lsm_d->lsm_session) {
  712. dev_err(rtd->dev,
  713. "%s: session allocation failed",
  714. __func__);
  715. rc = -EINVAL;
  716. goto fail_session_alloc;
  717. }
  718. /* Explicitly Assign the LAB thread to STOP state */
  719. lsm_d->lab.thread_status = MSM_LSM_LAB_THREAD_STOP;
  720. lsm_d->lsm_session->started = false;
  721. lsm_d->substream = substream;
  722. init_waitqueue_head(&lsm_d->lab.period_wait);
  723. lsm_d->cpe_prepared = false;
  724. dev_dbg(rtd->dev, "%s: allocated session with id = %d\n",
  725. __func__, lsm_d->lsm_session->id);
  726. rc = lsm_ops->lsm_open_tx(cpe->core_handle, lsm_d->lsm_session,
  727. LSM_VOICE_WAKEUP_APP_V2, 16000);
  728. if (rc < 0) {
  729. dev_err(rtd->dev,
  730. "%s: OPEN_TX cmd failed, err = %d\n",
  731. __func__, rc);
  732. goto fail_open_tx;
  733. }
  734. init_waitqueue_head(&lsm_d->event_wait);
  735. atomic_set(&lsm_d->event_avail, 0);
  736. atomic_set(&lsm_d->event_stop, 0);
  737. runtime->private_data = lsm_d;
  738. return 0;
  739. fail_open_tx:
  740. lsm_ops->lsm_dealloc_session(cpe->core_handle, lsm_d->lsm_session);
  741. fail_session_alloc:
  742. mutex_destroy(&lsm_d->lsm_api_lock);
  743. kfree(lsm_d);
  744. fail_return:
  745. return rc;
  746. }
  747. /*
  748. * msm_cpe_lsm_close: ASoC call to close/cleanup the stream
  749. * @substream: substream that is to be closed
  750. *
  751. * Deallocate the session and release the AFE port. It is not
  752. * required to deregister the sound model as long as we close
  753. * the lsm session on CPE.
  754. */
  755. static int msm_cpe_lsm_close(struct snd_pcm_substream *substream)
  756. {
  757. struct snd_pcm_runtime *runtime = substream->runtime;
  758. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  759. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  760. struct cpe_priv *cpe = cpe_get_private_data(substream);
  761. struct wcd_cpe_lsm_ops *lsm_ops;
  762. struct cpe_lsm_session *session;
  763. struct wcd_cpe_afe_ops *afe_ops;
  764. struct wcd_cpe_afe_port_cfg *afe_cfg;
  765. int rc = 0;
  766. if (!cpe || !cpe->core_handle) {
  767. dev_err(rtd->dev,
  768. "%s: Invalid private data\n",
  769. __func__);
  770. return -EINVAL;
  771. }
  772. if (!lsm_d || !lsm_d->lsm_session) {
  773. dev_err(rtd->dev,
  774. "%s: Invalid session data\n",
  775. __func__);
  776. return -EINVAL;
  777. }
  778. lsm_ops = &cpe->lsm_ops;
  779. session = lsm_d->lsm_session;
  780. afe_ops = &cpe->afe_ops;
  781. afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
  782. /*
  783. * If driver is closed without stopping LAB,
  784. * explicitly stop LAB before cleaning up the
  785. * driver resources.
  786. */
  787. rc = msm_cpe_lsm_lab_stop(substream);
  788. if (rc) {
  789. dev_err(rtd->dev,
  790. "%s: Failed to stop lab, error = %d\n",
  791. __func__, rc);
  792. return rc;
  793. }
  794. rc = msm_cpe_afe_port_cntl(substream,
  795. cpe->core_handle,
  796. afe_ops, afe_cfg,
  797. AFE_CMD_PORT_STOP);
  798. lsm_d->cpe_prepared = false;
  799. rc = lsm_ops->lsm_close_tx(cpe->core_handle, session);
  800. if (rc != 0) {
  801. dev_err(rtd->dev,
  802. "%s: lsm_close fail, err = %d\n",
  803. __func__, rc);
  804. return rc;
  805. }
  806. lsm_ops->lsm_dealloc_session(cpe->core_handle, session);
  807. runtime->private_data = NULL;
  808. mutex_destroy(&lsm_d->lsm_api_lock);
  809. kfree(lsm_d);
  810. return rc;
  811. }
  812. static int msm_cpe_lsm_get_conf_levels(
  813. struct cpe_lsm_session *session,
  814. u8 *conf_levels_ptr)
  815. {
  816. int rc = 0;
  817. if (session->num_confidence_levels <= 0) {
  818. pr_debug("%s: conf_levels (%u), skip set params\n",
  819. __func__,
  820. session->num_confidence_levels);
  821. goto done;
  822. }
  823. session->conf_levels = kzalloc(session->num_confidence_levels,
  824. GFP_KERNEL);
  825. if (!session->conf_levels) {
  826. rc = -ENOMEM;
  827. goto done;
  828. }
  829. if (copy_from_user(session->conf_levels,
  830. conf_levels_ptr,
  831. session->num_confidence_levels)) {
  832. pr_err("%s: copy_from_user failed for confidence levels %u\n",
  833. __func__, session->num_confidence_levels);
  834. kfree(session->conf_levels);
  835. session->conf_levels = NULL;
  836. rc = -EFAULT;
  837. goto done;
  838. }
  839. done:
  840. return rc;
  841. }
  842. static int msm_cpe_lsm_validate_out_format(
  843. struct snd_pcm_substream *substream,
  844. struct snd_lsm_output_format_cfg *cfg)
  845. {
  846. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  847. int rc = 0;
  848. if (!cfg) {
  849. dev_err(rtd->dev,
  850. "%s: Invalid lsm out cfg\n", __func__);
  851. rc = -EINVAL;
  852. goto done;
  853. }
  854. if (cfg->format != LSM_OUT_FORMAT_PCM &&
  855. cfg->format != LSM_OUT_FORMAT_ADPCM) {
  856. dev_err(rtd->dev,
  857. "%s: Invalid format %u\n",
  858. __func__, cfg->format);
  859. rc = -EINVAL;
  860. goto done;
  861. }
  862. if (cfg->packing != LSM_OUT_DATA_RAW &&
  863. cfg->packing != LSM_OUT_DATA_PACKED) {
  864. dev_err(rtd->dev,
  865. "%s: Invalid packing method %u\n",
  866. __func__, cfg->packing);
  867. rc = -EINVAL;
  868. goto done;
  869. }
  870. if (cfg->events != LSM_OUT_DATA_EVENTS_DISABLED &&
  871. cfg->events != LSM_OUT_DATA_EVENTS_ENABLED) {
  872. dev_err(rtd->dev,
  873. "%s: Invalid events provided %u\n",
  874. __func__, cfg->events);
  875. rc = -EINVAL;
  876. goto done;
  877. }
  878. if (cfg->mode != LSM_OUT_TRANSFER_MODE_RT &&
  879. cfg->mode != LSM_OUT_TRANSFER_MODE_FTRT) {
  880. dev_err(rtd->dev,
  881. "%s: Invalid transfer mode %u\n",
  882. __func__, cfg->mode);
  883. rc = -EINVAL;
  884. goto done;
  885. }
  886. done:
  887. return rc;
  888. }
  889. /*
  890. * msm_cpe_lsm_ioctl_shared: Shared IOCTL for this platform driver
  891. * @substream: ASoC substream for which the operation is invoked
  892. * @cmd: command for the ioctl
  893. * @arg: argument for the ioctl
  894. *
  895. * Perform dedicated listen functions like register sound model,
  896. * deregister sound model, etc
  897. * Called with lsm_api_lock acquired.
  898. */
  899. static int msm_cpe_lsm_ioctl_shared(struct snd_pcm_substream *substream,
  900. unsigned int cmd, void *arg)
  901. {
  902. struct snd_lsm_sound_model_v2 snd_model;
  903. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  904. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  905. struct cpe_priv *cpe = cpe_get_private_data(substream);
  906. struct cpe_lsm_session *session;
  907. struct wcd_cpe_lsm_ops *lsm_ops;
  908. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  909. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  910. struct msm_slim_dma_data *dma_data = NULL;
  911. struct snd_lsm_detection_params det_params;
  912. int rc = 0;
  913. if (!cpe || !cpe->core_handle) {
  914. dev_err(rtd->dev,
  915. "%s: Invalid private data\n",
  916. __func__);
  917. return -EINVAL;
  918. }
  919. if (!lsm_d || !lsm_d->lsm_session) {
  920. dev_err(rtd->dev,
  921. "%s: Invalid session data\n",
  922. __func__);
  923. return -EINVAL;
  924. }
  925. session = lsm_d->lsm_session;
  926. lsm_ops = &cpe->lsm_ops;
  927. switch (cmd) {
  928. case SNDRV_LSM_STOP_LAB:
  929. dev_dbg(rtd->dev,
  930. "%s: %s, lab_enable = %d, lab_thread_ststus = %d\n",
  931. __func__, "SNDRV_LSM_STOP_LAB",
  932. session->lab_enable,
  933. lab_d->thread_status);
  934. if (session->lab_enable &&
  935. lab_d->thread_status != MSM_LSM_LAB_THREAD_STOP) {
  936. atomic_inc(&lab_d->abort_read);
  937. wake_up(&lab_d->period_wait);
  938. rc = msm_cpe_lsm_lab_stop(substream);
  939. if (rc) {
  940. dev_err(rtd->dev,
  941. "%s: stop LAB failed, error = %d\n",
  942. __func__, rc);
  943. return rc;
  944. }
  945. } else if (!session->lab_enable) {
  946. dev_dbg(rtd->dev,
  947. "%s: LAB already stopped\n",
  948. __func__);
  949. }
  950. break;
  951. case SNDRV_LSM_LAB_CONTROL:
  952. if (copy_from_user(&session->lab_enable, (void *)arg,
  953. sizeof(u32))) {
  954. dev_err(rtd->dev,
  955. "%s: copy_from_user failed, size %zd\n",
  956. __func__, sizeof(u32));
  957. return -EFAULT;
  958. }
  959. dev_dbg(rtd->dev,
  960. "%s: %s, lab_enable = %d\n",
  961. __func__, "SNDRV_LSM_LAB_CONTROL",
  962. session->lab_enable);
  963. if (rtd->cpu_dai)
  964. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
  965. substream);
  966. if (!dma_data || !dma_data->dai_channel_ctl) {
  967. dev_err(rtd->dev,
  968. "%s: dma_data is not set\n", __func__);
  969. return -EINVAL;
  970. }
  971. if (session->lab_enable) {
  972. rc = msm_cpe_lab_buf_alloc(substream,
  973. session, dma_data);
  974. if (rc < 0) {
  975. dev_err(rtd->dev,
  976. "%s: lab buffer alloc failed, err = %d\n",
  977. __func__, rc);
  978. return rc;
  979. }
  980. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  981. dma_buf->dev.dev = substream->pcm->card->dev;
  982. dma_buf->private_data = NULL;
  983. dma_buf->area = lab_d->pcm_buf[0].mem;
  984. dma_buf->addr = lab_d->pcm_buf[0].phys;
  985. dma_buf->bytes = (lsm_d->hw_params.buf_sz *
  986. lsm_d->hw_params.period_count);
  987. init_completion(&lab_d->thread_complete);
  988. snd_pcm_set_runtime_buffer(substream,
  989. &substream->dma_buffer);
  990. rc = lsm_ops->lsm_lab_control(cpe->core_handle,
  991. session, true);
  992. if (rc < 0) {
  993. dev_err(rtd->dev,
  994. "%s: Lab Enable Failed rc %d\n",
  995. __func__, rc);
  996. return rc;
  997. }
  998. } else {
  999. /*
  1000. * It is possible that lab is still enabled
  1001. * when trying to de-allocate the lab buffer.
  1002. * Make sure to disable lab before de-allocating
  1003. * the lab buffer.
  1004. */
  1005. rc = msm_cpe_lsm_lab_stop(substream);
  1006. if (rc < 0) {
  1007. dev_err(rtd->dev,
  1008. "%s: LAB stop failed, error = %d\n",
  1009. __func__, rc);
  1010. return rc;
  1011. }
  1012. /*
  1013. * Buffer has to be de-allocated even if
  1014. * lab_control failed.
  1015. */
  1016. rc = msm_cpe_lab_buf_dealloc(substream,
  1017. session, dma_data);
  1018. if (rc < 0) {
  1019. dev_err(rtd->dev,
  1020. "%s: lab buffer free failed, err = %d\n",
  1021. __func__, rc);
  1022. return rc;
  1023. }
  1024. }
  1025. break;
  1026. case SNDRV_LSM_REG_SND_MODEL_V2:
  1027. dev_dbg(rtd->dev,
  1028. "%s: %s\n",
  1029. __func__, "SNDRV_LSM_REG_SND_MODEL_V2");
  1030. memcpy(&snd_model, arg,
  1031. sizeof(struct snd_lsm_sound_model_v2));
  1032. session->num_confidence_levels =
  1033. snd_model.num_confidence_levels;
  1034. rc = msm_cpe_lsm_get_conf_levels(session,
  1035. snd_model.confidence_level);
  1036. if (rc) {
  1037. dev_err(rtd->dev,
  1038. "%s: %s get_conf_levels fail, err = %d\n",
  1039. __func__, "SNDRV_LSM_REG_SND_MODEL_V2",
  1040. rc);
  1041. break;
  1042. }
  1043. session->snd_model_data = kzalloc(snd_model.data_size,
  1044. GFP_KERNEL);
  1045. if (!session->snd_model_data) {
  1046. kfree(session->conf_levels);
  1047. session->conf_levels = NULL;
  1048. return -ENOMEM;
  1049. }
  1050. session->snd_model_size = snd_model.data_size;
  1051. if (copy_from_user(session->snd_model_data,
  1052. snd_model.data, snd_model.data_size)) {
  1053. dev_err(rtd->dev,
  1054. "%s: copy_from_user failed for snd_model\n",
  1055. __func__);
  1056. kfree(session->conf_levels);
  1057. kfree(session->snd_model_data);
  1058. session->conf_levels = NULL;
  1059. session->snd_model_data = NULL;
  1060. return -EFAULT;
  1061. }
  1062. rc = lsm_ops->lsm_shmem_alloc(cpe->core_handle, session,
  1063. session->snd_model_size);
  1064. if (rc != 0) {
  1065. dev_err(rtd->dev,
  1066. "%s: shared memory allocation failed, err = %d\n",
  1067. __func__, rc);
  1068. kfree(session->snd_model_data);
  1069. kfree(session->conf_levels);
  1070. session->snd_model_data = NULL;
  1071. session->conf_levels = NULL;
  1072. return rc;
  1073. }
  1074. rc = lsm_ops->lsm_register_snd_model(cpe->core_handle, session,
  1075. snd_model.detection_mode,
  1076. snd_model.detect_failure);
  1077. if (rc != 0) {
  1078. dev_err(rtd->dev,
  1079. "%s: snd_model_reg failed, err = %d\n",
  1080. __func__, rc);
  1081. lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
  1082. kfree(session->snd_model_data);
  1083. kfree(session->conf_levels);
  1084. session->snd_model_data = NULL;
  1085. session->conf_levels = NULL;
  1086. return rc;
  1087. }
  1088. break;
  1089. case SNDRV_LSM_DEREG_SND_MODEL:
  1090. dev_dbg(rtd->dev,
  1091. "%s: %s\n",
  1092. __func__, "SNDRV_LSM_DEREG_SND_MODEL");
  1093. if (session->lab_enable) {
  1094. /*
  1095. * It is possible that lab is still enabled
  1096. * when trying to deregister sound model.
  1097. * Make sure to disable lab before de-allocating
  1098. * the lab buffer.
  1099. */
  1100. rc = msm_cpe_lsm_lab_stop(substream);
  1101. if (rc) {
  1102. dev_err(rtd->dev,
  1103. "%s: LAB stop failed, error = %d\n",
  1104. __func__, rc);
  1105. return rc;
  1106. }
  1107. rc = lsm_ops->lsm_lab_control(cpe->core_handle,
  1108. session, false);
  1109. if (rc)
  1110. dev_err(rtd->dev,
  1111. "%s: Lab Disable Failed rc %d\n",
  1112. __func__, rc);
  1113. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
  1114. substream);
  1115. if (!dma_data || !dma_data->dai_channel_ctl)
  1116. dev_err(rtd->dev,
  1117. "%s: dma_data is not set\n", __func__);
  1118. /*
  1119. * Buffer has to be de-allocated even if
  1120. * lab_control failed and/or dma data is invalid.
  1121. */
  1122. rc = msm_cpe_lab_buf_dealloc(substream,
  1123. session, dma_data);
  1124. if (rc < 0)
  1125. dev_err(rtd->dev,
  1126. "%s: lab buffer free failed, err = %d\n",
  1127. __func__, rc);
  1128. }
  1129. rc = lsm_ops->lsm_deregister_snd_model(
  1130. cpe->core_handle, session);
  1131. if (rc != 0) {
  1132. dev_err(rtd->dev,
  1133. "%s: snd_model de-reg failed, err = %d\n",
  1134. __func__, rc);
  1135. return rc;
  1136. }
  1137. kfree(session->snd_model_data);
  1138. kfree(session->conf_levels);
  1139. session->snd_model_data = NULL;
  1140. session->conf_levels = NULL;
  1141. rc = lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
  1142. if (rc != 0) {
  1143. dev_err(rtd->dev,
  1144. "%s: LSM shared memory dealloc failed, err = %d\n",
  1145. __func__, rc);
  1146. return rc;
  1147. }
  1148. break;
  1149. case SNDRV_LSM_EVENT_STATUS:
  1150. case SNDRV_LSM_EVENT_STATUS_V3: {
  1151. struct snd_lsm_event_status *user;
  1152. struct snd_lsm_event_status_v3 *user_v3;
  1153. dev_dbg(rtd->dev,
  1154. "%s: %s\n",
  1155. __func__, "SNDRV_LSM_EVENT_STATUS(_V3)");
  1156. if (!arg) {
  1157. dev_err(rtd->dev,
  1158. "%s: Invalid argument to ioctl %s\n",
  1159. __func__,
  1160. "SNDRV_LSM_EVENT_STATUS(_V3)");
  1161. return -EINVAL;
  1162. }
  1163. /*
  1164. * Release the api lock before wait to allow
  1165. * other IOCTLs to be invoked while waiting
  1166. * for event
  1167. */
  1168. MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
  1169. "lsm_api_lock");
  1170. rc = wait_event_freezable(lsm_d->event_wait,
  1171. (atomic_read(&lsm_d->event_avail) == 1) ||
  1172. (atomic_read(&lsm_d->event_stop) == 1));
  1173. MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
  1174. "lsm_api_lock");
  1175. if (!rc) {
  1176. if (atomic_read(&lsm_d->event_avail) == 1) {
  1177. rc = 0;
  1178. atomic_set(&lsm_d->event_avail, 0);
  1179. if (cmd == SNDRV_LSM_EVENT_STATUS) {
  1180. user = arg;
  1181. if (lsm_d->ev_det_pld_size >
  1182. user->payload_size) {
  1183. dev_err(rtd->dev,
  1184. "%s: avail pld_bytes = %u, needed = %u\n",
  1185. __func__,
  1186. user->payload_size,
  1187. lsm_d->ev_det_pld_size);
  1188. return -EINVAL;
  1189. }
  1190. user->status = lsm_d->ev_det_status;
  1191. user->payload_size =
  1192. lsm_d->ev_det_pld_size;
  1193. memcpy(user->payload,
  1194. lsm_d->ev_det_payload,
  1195. lsm_d->ev_det_pld_size);
  1196. } else {
  1197. user_v3 = arg;
  1198. if (lsm_d->ev_det_pld_size >
  1199. user_v3->payload_size) {
  1200. dev_err(rtd->dev,
  1201. "%s: avail pld_bytes = %u, needed = %u\n",
  1202. __func__,
  1203. user_v3->payload_size,
  1204. lsm_d->ev_det_pld_size);
  1205. return -EINVAL;
  1206. }
  1207. /* event status timestamp not supported
  1208. * on CPE mode. Set msw and lsw to 0.
  1209. */
  1210. user_v3->timestamp_lsw = 0;
  1211. user_v3->timestamp_msw = 0;
  1212. user_v3->status = lsm_d->ev_det_status;
  1213. user_v3->payload_size =
  1214. lsm_d->ev_det_pld_size;
  1215. memcpy(user_v3->payload,
  1216. lsm_d->ev_det_payload,
  1217. lsm_d->ev_det_pld_size);
  1218. }
  1219. } else if (atomic_read(&lsm_d->event_stop) == 1) {
  1220. dev_dbg(rtd->dev,
  1221. "%s: wait_aborted\n", __func__);
  1222. if (cmd == SNDRV_LSM_EVENT_STATUS) {
  1223. user = arg;
  1224. user->payload_size = 0;
  1225. } else {
  1226. user_v3 = arg;
  1227. user_v3->payload_size = 0;
  1228. }
  1229. rc = 0;
  1230. }
  1231. }
  1232. }
  1233. break;
  1234. case SNDRV_LSM_ABORT_EVENT:
  1235. dev_dbg(rtd->dev,
  1236. "%s: %s\n",
  1237. __func__, "SNDRV_LSM_ABORT_EVENT");
  1238. atomic_set(&lsm_d->event_stop, 1);
  1239. wake_up(&lsm_d->event_wait);
  1240. break;
  1241. case SNDRV_LSM_START:
  1242. dev_dbg(rtd->dev,
  1243. "%s: %s\n",
  1244. __func__, "SNDRV_LSM_START");
  1245. rc = lsm_ops->lsm_start(cpe->core_handle, session);
  1246. if (rc != 0) {
  1247. dev_err(rtd->dev,
  1248. "%s: lsm_start fail, err = %d\n",
  1249. __func__, rc);
  1250. return rc;
  1251. }
  1252. session->started = true;
  1253. break;
  1254. case SNDRV_LSM_STOP:
  1255. dev_dbg(rtd->dev,
  1256. "%s: %s, lab_enable = %d, lab_thread_status = %d\n",
  1257. __func__, "SNDRV_LSM_STOP",
  1258. session->lab_enable,
  1259. lab_d->thread_status);
  1260. if ((session->lab_enable &&
  1261. lab_d->thread_status ==
  1262. MSM_LSM_LAB_THREAD_RUNNING)) {
  1263. /* Explicitly stop LAB */
  1264. rc = msm_cpe_lsm_lab_stop(substream);
  1265. if (rc) {
  1266. dev_err(rtd->dev,
  1267. "%s: lab_stop failed, err = %d\n",
  1268. __func__, rc);
  1269. return rc;
  1270. }
  1271. }
  1272. rc = lsm_ops->lsm_stop(cpe->core_handle, session);
  1273. if (rc != 0) {
  1274. dev_err(rtd->dev,
  1275. "%s: lsm_stop fail err = %d\n",
  1276. __func__, rc);
  1277. return rc;
  1278. }
  1279. session->started = false;
  1280. break;
  1281. case SNDRV_LSM_SET_PARAMS:
  1282. memcpy(&det_params, arg,
  1283. sizeof(det_params));
  1284. if (det_params.num_confidence_levels <= 0) {
  1285. dev_err(rtd->dev,
  1286. "%s: %s: Invalid confidence levels %u\n",
  1287. __func__, "SNDRV_LSM_SET_PARAMS",
  1288. det_params.num_confidence_levels);
  1289. return -EINVAL;
  1290. }
  1291. session->num_confidence_levels =
  1292. det_params.num_confidence_levels;
  1293. rc = msm_cpe_lsm_get_conf_levels(session,
  1294. det_params.conf_level);
  1295. if (rc) {
  1296. dev_err(rtd->dev,
  1297. "%s: %s get_conf_levels fail, err = %d\n",
  1298. __func__, "SNDRV_LSM_SET_PARAMS",
  1299. rc);
  1300. break;
  1301. }
  1302. rc = lsm_ops->lsm_set_data(cpe->core_handle, session,
  1303. det_params.detect_mode,
  1304. det_params.detect_failure);
  1305. if (rc) {
  1306. dev_err(rtd->dev,
  1307. "%s: lsm_set_data failed, err = %d\n",
  1308. __func__, rc);
  1309. return rc;
  1310. }
  1311. kfree(session->conf_levels);
  1312. session->conf_levels = NULL;
  1313. break;
  1314. case SNDRV_LSM_OUT_FORMAT_CFG: {
  1315. struct snd_lsm_output_format_cfg u_fmt_cfg;
  1316. if (!arg) {
  1317. dev_err(rtd->dev,
  1318. "%s: Invalid argument to ioctl %s\n",
  1319. __func__, "SNDRV_LSM_OUT_FORMAT_CFG");
  1320. return -EINVAL;
  1321. }
  1322. if (copy_from_user(&u_fmt_cfg, arg,
  1323. sizeof(u_fmt_cfg))) {
  1324. dev_err(rtd->dev,
  1325. "%s: copy_from_user failed for out_fmt_cfg\n",
  1326. __func__);
  1327. return -EFAULT;
  1328. }
  1329. if (msm_cpe_lsm_validate_out_format(substream,
  1330. &u_fmt_cfg))
  1331. return -EINVAL;
  1332. session->out_fmt_cfg.format = u_fmt_cfg.format;
  1333. session->out_fmt_cfg.pack_mode = u_fmt_cfg.packing;
  1334. session->out_fmt_cfg.data_path_events =
  1335. u_fmt_cfg.events;
  1336. session->out_fmt_cfg.transfer_mode = u_fmt_cfg.mode;
  1337. rc = lsm_ops->lsm_set_fmt_cfg(cpe->core_handle,
  1338. session);
  1339. if (rc) {
  1340. dev_err(rtd->dev,
  1341. "%s: lsm_set_fmt_cfg failed, err = %d\n",
  1342. __func__, rc);
  1343. return rc;
  1344. }
  1345. }
  1346. break;
  1347. case SNDRV_LSM_SET_PORT: {
  1348. u32 port_id = cpe->input_port_id;
  1349. dev_dbg(rtd->dev, "%s: %s\n", __func__, "SNDRV_LSM_SET_PORT");
  1350. rc = lsm_ops->lsm_set_port(cpe->core_handle, session, &port_id);
  1351. if (rc) {
  1352. dev_err(rtd->dev,
  1353. "%s: lsm_set_port failed, err = %d\n",
  1354. __func__, rc);
  1355. return rc;
  1356. }
  1357. }
  1358. break;
  1359. default:
  1360. dev_dbg(rtd->dev,
  1361. "%s: Default snd_lib_ioctl cmd 0x%x\n",
  1362. __func__, cmd);
  1363. rc = snd_pcm_lib_ioctl(substream, cmd, arg);
  1364. }
  1365. return rc;
  1366. }
  1367. static int msm_cpe_lsm_lab_start(struct snd_pcm_substream *substream,
  1368. u16 event_det_status)
  1369. {
  1370. struct snd_soc_pcm_runtime *rtd;
  1371. struct cpe_lsm_data *lsm_d = NULL;
  1372. struct cpe_priv *cpe = NULL;
  1373. struct cpe_lsm_session *session = NULL;
  1374. struct cpe_lsm_lab *lab_d = NULL;
  1375. struct cpe_hw_params *hw_params;
  1376. struct wcd_cpe_lsm_ops *lsm_ops;
  1377. struct wcd_cpe_afe_ops *afe_ops;
  1378. struct wcd_cpe_afe_port_cfg *out_port;
  1379. int rc;
  1380. if (!substream || !substream->private_data) {
  1381. pr_err("%s: invalid substream (%pK)\n",
  1382. __func__, substream);
  1383. return -EINVAL;
  1384. }
  1385. rtd = substream->private_data;
  1386. lsm_d = cpe_get_lsm_data(substream);
  1387. cpe = cpe_get_private_data(substream);
  1388. if (!cpe || !cpe->core_handle) {
  1389. dev_err(rtd->dev,
  1390. "%s: Invalid private data\n",
  1391. __func__);
  1392. return -EINVAL;
  1393. }
  1394. if (!lsm_d || !lsm_d->lsm_session) {
  1395. dev_err(rtd->dev,
  1396. "%s: Invalid session data\n",
  1397. __func__);
  1398. return -EINVAL;
  1399. }
  1400. session = lsm_d->lsm_session;
  1401. lsm_ops = &cpe->lsm_ops;
  1402. lab_d = &lsm_d->lab;
  1403. afe_ops = &cpe->afe_ops;
  1404. hw_params = &lsm_d->hw_params;
  1405. if (!session->started) {
  1406. dev_dbg(rtd->dev,
  1407. "%s: Session is stopped, cannot start LAB\n",
  1408. __func__);
  1409. return 0;
  1410. }
  1411. reinit_completion(&lab_d->thread_complete);
  1412. if (session->lab_enable &&
  1413. event_det_status ==
  1414. LSM_VOICE_WAKEUP_STATUS_DETECTED) {
  1415. out_port = &session->afe_out_port_cfg;
  1416. out_port->port_id = session->afe_out_port_id;
  1417. out_port->bit_width = hw_params->sample_size;
  1418. out_port->num_channels = hw_params->channels;
  1419. out_port->sample_rate = hw_params->sample_rate;
  1420. dev_dbg(rtd->dev, "%s: port_id= %u, bit_width= %u, rate= %u\n",
  1421. __func__, out_port->port_id, out_port->bit_width,
  1422. out_port->sample_rate);
  1423. rc = afe_ops->afe_port_cmd_cfg(cpe->core_handle,
  1424. out_port);
  1425. if (rc) {
  1426. dev_err(rtd->dev,
  1427. "%s: Failed afe generic config v2, err = %d\n",
  1428. __func__, rc);
  1429. return rc;
  1430. }
  1431. atomic_set(&lab_d->abort_read, 0);
  1432. dev_dbg(rtd->dev,
  1433. "%s: KW detected, scheduling LAB thread\n",
  1434. __func__);
  1435. /*
  1436. * Even though thread might be only scheduled and
  1437. * not currently running, mark the internal driver
  1438. * status to running so driver can cancel this thread
  1439. * if it needs to before the thread gets chance to run.
  1440. */
  1441. lab_d->thread_status = MSM_LSM_LAB_THREAD_RUNNING;
  1442. session->lsm_lab_thread = kthread_run(
  1443. msm_cpe_lab_thread,
  1444. lsm_d,
  1445. "lab_thread");
  1446. }
  1447. return 0;
  1448. }
  1449. static bool msm_cpe_lsm_is_valid_stream(struct snd_pcm_substream *substream,
  1450. const char *func)
  1451. {
  1452. struct snd_soc_pcm_runtime *rtd;
  1453. struct cpe_lsm_data *lsm_d = NULL;
  1454. struct cpe_priv *cpe = NULL;
  1455. struct cpe_lsm_session *session = NULL;
  1456. struct wcd_cpe_lsm_ops *lsm_ops;
  1457. if (!substream || !substream->private_data) {
  1458. pr_err("%s: invalid substream (%pK)\n",
  1459. func, substream);
  1460. return false;
  1461. }
  1462. rtd = substream->private_data;
  1463. lsm_d = cpe_get_lsm_data(substream);
  1464. cpe = cpe_get_private_data(substream);
  1465. if (!cpe || !cpe->core_handle) {
  1466. dev_err(rtd->dev,
  1467. "%s: Invalid private data\n",
  1468. func);
  1469. return false;
  1470. }
  1471. if (!lsm_d || !lsm_d->lsm_session) {
  1472. dev_err(rtd->dev,
  1473. "%s: Invalid session data\n",
  1474. func);
  1475. return false;
  1476. }
  1477. session = lsm_d->lsm_session;
  1478. lsm_ops = &cpe->lsm_ops;
  1479. if (!lsm_ops) {
  1480. dev_err(rtd->dev,
  1481. "%s: Invalid lsm_ops\n", func);
  1482. return false;
  1483. }
  1484. return true;
  1485. }
  1486. static int msm_cpe_lsm_set_epd(struct snd_pcm_substream *substream,
  1487. struct lsm_params_info *p_info)
  1488. {
  1489. struct snd_soc_pcm_runtime *rtd;
  1490. struct cpe_lsm_data *lsm_d = NULL;
  1491. struct cpe_priv *cpe = NULL;
  1492. struct cpe_lsm_session *session = NULL;
  1493. struct wcd_cpe_lsm_ops *lsm_ops;
  1494. struct snd_lsm_ep_det_thres epd_thres;
  1495. int rc;
  1496. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1497. return -EINVAL;
  1498. rtd = substream->private_data;
  1499. lsm_d = cpe_get_lsm_data(substream);
  1500. cpe = cpe_get_private_data(substream);
  1501. session = lsm_d->lsm_session;
  1502. lsm_ops = &cpe->lsm_ops;
  1503. if (p_info->param_size != sizeof(epd_thres)) {
  1504. dev_err(rtd->dev,
  1505. "%s: Invalid param_size %d\n",
  1506. __func__, p_info->param_size);
  1507. rc = -EINVAL;
  1508. goto done;
  1509. }
  1510. if (copy_from_user(&epd_thres, p_info->param_data,
  1511. p_info->param_size)) {
  1512. dev_err(rtd->dev,
  1513. "%s: copy_from_user failed, size = %d\n",
  1514. __func__, p_info->param_size);
  1515. rc = -EFAULT;
  1516. goto done;
  1517. }
  1518. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1519. session, p_info, &epd_thres,
  1520. LSM_ENDPOINT_DETECT_THRESHOLD);
  1521. if (unlikely(rc))
  1522. dev_err(rtd->dev,
  1523. "%s: set_one_param(epd_threshold) failed, rc %d\n",
  1524. __func__, rc);
  1525. done:
  1526. return rc;
  1527. }
  1528. static int msm_cpe_lsm_set_mode(struct snd_pcm_substream *substream,
  1529. struct lsm_params_info *p_info)
  1530. {
  1531. struct snd_soc_pcm_runtime *rtd;
  1532. struct cpe_lsm_data *lsm_d = NULL;
  1533. struct cpe_priv *cpe = NULL;
  1534. struct cpe_lsm_session *session = NULL;
  1535. struct wcd_cpe_lsm_ops *lsm_ops;
  1536. struct snd_lsm_detect_mode det_mode;
  1537. int rc;
  1538. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1539. return -EINVAL;
  1540. rtd = substream->private_data;
  1541. lsm_d = cpe_get_lsm_data(substream);
  1542. cpe = cpe_get_private_data(substream);
  1543. session = lsm_d->lsm_session;
  1544. lsm_ops = &cpe->lsm_ops;
  1545. if (p_info->param_size != sizeof(det_mode)) {
  1546. dev_err(rtd->dev,
  1547. "%s: Invalid param_size %d\n",
  1548. __func__, p_info->param_size);
  1549. rc = -EINVAL;
  1550. goto done;
  1551. }
  1552. if (copy_from_user(&det_mode, p_info->param_data,
  1553. p_info->param_size)) {
  1554. dev_err(rtd->dev,
  1555. "%s: copy_from_user failed, size = %d\n",
  1556. __func__, p_info->param_size);
  1557. rc = -EFAULT;
  1558. goto done;
  1559. }
  1560. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1561. session, p_info, &det_mode,
  1562. LSM_OPERATION_MODE);
  1563. if (unlikely(rc))
  1564. dev_err(rtd->dev,
  1565. "%s: set_one_param(epd_threshold) failed, rc %d\n",
  1566. __func__, rc);
  1567. done:
  1568. return rc;
  1569. }
  1570. static int msm_cpe_lsm_set_gain(struct snd_pcm_substream *substream,
  1571. struct lsm_params_info *p_info)
  1572. {
  1573. struct snd_soc_pcm_runtime *rtd;
  1574. struct cpe_lsm_data *lsm_d = NULL;
  1575. struct cpe_priv *cpe = NULL;
  1576. struct cpe_lsm_session *session = NULL;
  1577. struct wcd_cpe_lsm_ops *lsm_ops;
  1578. struct snd_lsm_gain gain;
  1579. int rc;
  1580. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1581. return -EINVAL;
  1582. rtd = substream->private_data;
  1583. lsm_d = cpe_get_lsm_data(substream);
  1584. cpe = cpe_get_private_data(substream);
  1585. session = lsm_d->lsm_session;
  1586. lsm_ops = &cpe->lsm_ops;
  1587. if (p_info->param_size != sizeof(gain)) {
  1588. dev_err(rtd->dev,
  1589. "%s: Invalid param_size %d\n",
  1590. __func__, p_info->param_size);
  1591. rc = -EINVAL;
  1592. goto done;
  1593. }
  1594. if (copy_from_user(&gain, p_info->param_data,
  1595. p_info->param_size)) {
  1596. dev_err(rtd->dev,
  1597. "%s: copy_from_user failed, size = %d\n",
  1598. __func__, p_info->param_size);
  1599. rc = -EFAULT;
  1600. goto done;
  1601. }
  1602. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1603. session, p_info, &gain,
  1604. LSM_GAIN);
  1605. if (unlikely(rc))
  1606. dev_err(rtd->dev,
  1607. "%s: set_one_param(epd_threshold) failed, rc %d\n",
  1608. __func__, rc);
  1609. done:
  1610. return rc;
  1611. }
  1612. static int msm_cpe_lsm_set_conf(struct snd_pcm_substream *substream,
  1613. struct lsm_params_info *p_info)
  1614. {
  1615. struct snd_soc_pcm_runtime *rtd;
  1616. struct cpe_lsm_data *lsm_d = NULL;
  1617. struct cpe_priv *cpe = NULL;
  1618. struct cpe_lsm_session *session = NULL;
  1619. struct wcd_cpe_lsm_ops *lsm_ops;
  1620. int rc;
  1621. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1622. return -EINVAL;
  1623. rtd = substream->private_data;
  1624. lsm_d = cpe_get_lsm_data(substream);
  1625. cpe = cpe_get_private_data(substream);
  1626. session = lsm_d->lsm_session;
  1627. lsm_ops = &cpe->lsm_ops;
  1628. session->num_confidence_levels =
  1629. p_info->param_size;
  1630. rc = msm_cpe_lsm_get_conf_levels(session,
  1631. p_info->param_data);
  1632. if (rc) {
  1633. dev_err(rtd->dev,
  1634. "%s: get_conf_levels failed, err = %d\n",
  1635. __func__, rc);
  1636. goto done;
  1637. }
  1638. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1639. session, p_info, NULL,
  1640. LSM_MIN_CONFIDENCE_LEVELS);
  1641. if (unlikely(rc))
  1642. dev_err(rtd->dev,
  1643. "%s: set_one_param(conf_levels) failed, rc %d\n",
  1644. __func__, rc);
  1645. done:
  1646. return rc;
  1647. }
  1648. static int msm_cpe_lsm_reg_model(struct snd_pcm_substream *substream,
  1649. struct lsm_params_info *p_info)
  1650. {
  1651. struct snd_soc_pcm_runtime *rtd;
  1652. struct cpe_lsm_data *lsm_d = NULL;
  1653. struct cpe_priv *cpe = NULL;
  1654. struct cpe_lsm_session *session = NULL;
  1655. struct wcd_cpe_lsm_ops *lsm_ops;
  1656. int rc;
  1657. size_t offset;
  1658. u8 *snd_model_ptr;
  1659. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1660. return -EINVAL;
  1661. rtd = substream->private_data;
  1662. lsm_d = cpe_get_lsm_data(substream);
  1663. cpe = cpe_get_private_data(substream);
  1664. session = lsm_d->lsm_session;
  1665. lsm_ops = &cpe->lsm_ops;
  1666. lsm_ops->lsm_get_snd_model_offset(cpe->core_handle,
  1667. session, &offset);
  1668. /* Check if 'p_info->param_size + offset' crosses U32_MAX. */
  1669. if (p_info->param_size > U32_MAX - offset) {
  1670. dev_err(rtd->dev,
  1671. "%s: Invalid param_size %d\n",
  1672. __func__, p_info->param_size);
  1673. return -EINVAL;
  1674. }
  1675. session->snd_model_size = p_info->param_size + offset;
  1676. session->snd_model_data = vzalloc(session->snd_model_size);
  1677. if (!session->snd_model_data)
  1678. return -ENOMEM;
  1679. snd_model_ptr = ((u8 *) session->snd_model_data) + offset;
  1680. if (copy_from_user(snd_model_ptr,
  1681. p_info->param_data, p_info->param_size)) {
  1682. dev_err(rtd->dev,
  1683. "%s: copy_from_user for snd_model failed\n",
  1684. __func__);
  1685. rc = -EFAULT;
  1686. goto free_snd_model_data;
  1687. }
  1688. rc = lsm_ops->lsm_shmem_alloc(cpe->core_handle, session,
  1689. session->snd_model_size);
  1690. if (rc != 0) {
  1691. dev_err(rtd->dev,
  1692. "%s: shared memory allocation failed, err = %d\n",
  1693. __func__, rc);
  1694. rc = -EINVAL;
  1695. goto free_snd_model_data;
  1696. }
  1697. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1698. session, p_info, NULL,
  1699. LSM_REG_SND_MODEL);
  1700. if (unlikely(rc)) {
  1701. dev_err(rtd->dev,
  1702. "%s: set_one_param(snd_model) failed, rc %d\n",
  1703. __func__, rc);
  1704. goto dealloc_shmem;
  1705. }
  1706. return 0;
  1707. dealloc_shmem:
  1708. lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
  1709. free_snd_model_data:
  1710. vfree(session->snd_model_data);
  1711. return rc;
  1712. }
  1713. static int msm_cpe_lsm_dereg_model(struct snd_pcm_substream *substream,
  1714. struct lsm_params_info *p_info)
  1715. {
  1716. struct snd_soc_pcm_runtime *rtd;
  1717. struct cpe_lsm_data *lsm_d = NULL;
  1718. struct cpe_priv *cpe = NULL;
  1719. struct cpe_lsm_session *session = NULL;
  1720. struct wcd_cpe_lsm_ops *lsm_ops;
  1721. int rc;
  1722. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1723. return -EINVAL;
  1724. rtd = substream->private_data;
  1725. lsm_d = cpe_get_lsm_data(substream);
  1726. cpe = cpe_get_private_data(substream);
  1727. session = lsm_d->lsm_session;
  1728. lsm_ops = &cpe->lsm_ops;
  1729. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1730. session, p_info, NULL,
  1731. LSM_DEREG_SND_MODEL);
  1732. if (rc)
  1733. dev_err(rtd->dev,
  1734. "%s: dereg_snd_model failed\n",
  1735. __func__);
  1736. return lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
  1737. }
  1738. static int msm_cpe_lsm_set_custom(struct snd_pcm_substream *substream,
  1739. struct lsm_params_info *p_info)
  1740. {
  1741. struct snd_soc_pcm_runtime *rtd;
  1742. struct cpe_lsm_data *lsm_d = NULL;
  1743. struct cpe_priv *cpe = NULL;
  1744. struct cpe_lsm_session *session = NULL;
  1745. struct wcd_cpe_lsm_ops *lsm_ops;
  1746. u8 *data;
  1747. int rc;
  1748. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1749. return -EINVAL;
  1750. rtd = substream->private_data;
  1751. lsm_d = cpe_get_lsm_data(substream);
  1752. cpe = cpe_get_private_data(substream);
  1753. session = lsm_d->lsm_session;
  1754. lsm_ops = &cpe->lsm_ops;
  1755. if (p_info->param_size > MSM_CPE_MAX_CUSTOM_PARAM_SIZE) {
  1756. dev_err(rtd->dev,
  1757. "%s: invalid size %d, max allowed %d\n",
  1758. __func__, p_info->param_size,
  1759. MSM_CPE_MAX_CUSTOM_PARAM_SIZE);
  1760. return -EINVAL;
  1761. }
  1762. data = kzalloc(p_info->param_size, GFP_KERNEL);
  1763. if (!data)
  1764. return -ENOMEM;
  1765. if (copy_from_user(data, p_info->param_data,
  1766. p_info->param_size)) {
  1767. dev_err(rtd->dev,
  1768. "%s: copy_from_user failed for custom params, size = %d\n",
  1769. __func__, p_info->param_size);
  1770. rc = -EFAULT;
  1771. goto err_ret;
  1772. }
  1773. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1774. session, p_info, data,
  1775. LSM_CUSTOM_PARAMS);
  1776. if (rc)
  1777. dev_err(rtd->dev,
  1778. "%s: custom_params failed, err = %d\n",
  1779. __func__, rc);
  1780. err_ret:
  1781. kfree(data);
  1782. return rc;
  1783. }
  1784. static int msm_cpe_lsm_process_params(struct snd_pcm_substream *substream,
  1785. struct snd_lsm_module_params *p_data,
  1786. void *params)
  1787. {
  1788. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1789. struct lsm_params_info *p_info;
  1790. int i;
  1791. int rc = 0;
  1792. p_info = (struct lsm_params_info *) params;
  1793. for (i = 0; i < p_data->num_params; i++) {
  1794. dev_dbg(rtd->dev,
  1795. "%s: param (%d), module_id = 0x%x, param_id = 0x%x, param_size = 0x%x, param_type = 0x%x\n",
  1796. __func__, i, p_info->module_id,
  1797. p_info->param_id, p_info->param_size,
  1798. p_info->param_type);
  1799. switch (p_info->param_type) {
  1800. case LSM_ENDPOINT_DETECT_THRESHOLD:
  1801. rc = msm_cpe_lsm_set_epd(substream, p_info);
  1802. break;
  1803. case LSM_OPERATION_MODE:
  1804. rc = msm_cpe_lsm_set_mode(substream, p_info);
  1805. break;
  1806. case LSM_GAIN:
  1807. rc = msm_cpe_lsm_set_gain(substream, p_info);
  1808. break;
  1809. case LSM_MIN_CONFIDENCE_LEVELS:
  1810. rc = msm_cpe_lsm_set_conf(substream, p_info);
  1811. break;
  1812. case LSM_REG_SND_MODEL:
  1813. rc = msm_cpe_lsm_reg_model(substream, p_info);
  1814. break;
  1815. case LSM_DEREG_SND_MODEL:
  1816. rc = msm_cpe_lsm_dereg_model(substream, p_info);
  1817. break;
  1818. case LSM_CUSTOM_PARAMS:
  1819. rc = msm_cpe_lsm_set_custom(substream, p_info);
  1820. break;
  1821. default:
  1822. dev_err(rtd->dev,
  1823. "%s: Invalid param_type %d\n",
  1824. __func__, p_info->param_type);
  1825. rc = -EINVAL;
  1826. break;
  1827. }
  1828. if (rc) {
  1829. pr_err("%s: set_param fail for param_type %d\n",
  1830. __func__, p_info->param_type);
  1831. return rc;
  1832. }
  1833. p_info++;
  1834. }
  1835. return rc;
  1836. }
  1837. static int msm_cpe_lsm_ioctl(struct snd_pcm_substream *substream,
  1838. unsigned int cmd, void *arg)
  1839. {
  1840. int err = 0;
  1841. struct snd_soc_pcm_runtime *rtd;
  1842. struct cpe_priv *cpe = NULL;
  1843. struct cpe_lsm_data *lsm_d = NULL;
  1844. struct cpe_lsm_session *session = NULL;
  1845. struct wcd_cpe_lsm_ops *lsm_ops;
  1846. if (!substream || !substream->private_data) {
  1847. pr_err("%s: invalid substream (%pK)\n",
  1848. __func__, substream);
  1849. return -EINVAL;
  1850. }
  1851. rtd = substream->private_data;
  1852. lsm_d = cpe_get_lsm_data(substream);
  1853. cpe = cpe_get_private_data(substream);
  1854. if (!cpe || !cpe->core_handle) {
  1855. dev_err(rtd->dev,
  1856. "%s: Invalid private data\n",
  1857. __func__);
  1858. return -EINVAL;
  1859. }
  1860. if (!lsm_d || !lsm_d->lsm_session) {
  1861. dev_err(rtd->dev,
  1862. "%s: Invalid session data\n",
  1863. __func__);
  1864. return -EINVAL;
  1865. }
  1866. MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
  1867. "lsm_api_lock");
  1868. session = lsm_d->lsm_session;
  1869. lsm_ops = &cpe->lsm_ops;
  1870. switch (cmd) {
  1871. case SNDRV_LSM_REG_SND_MODEL_V2: {
  1872. struct snd_lsm_sound_model_v2 snd_model;
  1873. if (session->is_topology_used) {
  1874. dev_err(rtd->dev,
  1875. "%s: %s: not supported if using topology\n",
  1876. __func__, "LSM_REG_SND_MODEL_V2");
  1877. err = -EINVAL;
  1878. goto done;
  1879. }
  1880. if (copy_from_user(&snd_model, (void *)arg,
  1881. sizeof(struct snd_lsm_sound_model_v2))) {
  1882. dev_err(rtd->dev,
  1883. "%s: copy from user failed, size %zd\n",
  1884. __func__,
  1885. sizeof(struct snd_lsm_sound_model_v2));
  1886. err = -EFAULT;
  1887. goto done;
  1888. }
  1889. err = msm_cpe_lsm_ioctl_shared(substream, cmd,
  1890. &snd_model);
  1891. }
  1892. break;
  1893. case SNDRV_LSM_EVENT_STATUS: {
  1894. struct snd_lsm_event_status u_event_status;
  1895. struct snd_lsm_event_status *event_status = NULL;
  1896. int u_pld_size = 0;
  1897. if (copy_from_user(&u_event_status, (void *)arg,
  1898. sizeof(struct snd_lsm_event_status))) {
  1899. dev_err(rtd->dev,
  1900. "%s: event status copy from user failed, size %zd\n",
  1901. __func__,
  1902. sizeof(struct snd_lsm_event_status));
  1903. err = -EFAULT;
  1904. goto done;
  1905. }
  1906. if (u_event_status.payload_size >
  1907. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  1908. dev_err(rtd->dev,
  1909. "%s: payload_size %d is invalid, max allowed = %d\n",
  1910. __func__, u_event_status.payload_size,
  1911. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  1912. err = -EINVAL;
  1913. goto done;
  1914. }
  1915. u_pld_size = sizeof(struct snd_lsm_event_status) +
  1916. u_event_status.payload_size;
  1917. event_status = kzalloc(u_pld_size, GFP_KERNEL);
  1918. if (!event_status) {
  1919. err = -ENOMEM;
  1920. goto done;
  1921. } else {
  1922. event_status->payload_size =
  1923. u_event_status.payload_size;
  1924. err = msm_cpe_lsm_ioctl_shared(substream,
  1925. cmd, event_status);
  1926. }
  1927. if (!err && copy_to_user(arg, event_status, u_pld_size)) {
  1928. dev_err(rtd->dev,
  1929. "%s: copy to user failed\n",
  1930. __func__);
  1931. kfree(event_status);
  1932. err = -EFAULT;
  1933. goto done;
  1934. }
  1935. msm_cpe_lsm_lab_start(substream, event_status->status);
  1936. msm_cpe_process_event_status_done(lsm_d);
  1937. kfree(event_status);
  1938. }
  1939. break;
  1940. case SNDRV_LSM_EVENT_STATUS_V3: {
  1941. struct snd_lsm_event_status_v3 u_event_status;
  1942. struct snd_lsm_event_status_v3 *event_status = NULL;
  1943. int u_pld_size = 0;
  1944. if (copy_from_user(&u_event_status, (void *)arg,
  1945. sizeof(struct snd_lsm_event_status_v3))) {
  1946. dev_err(rtd->dev,
  1947. "%s: event status copy from user failed, size %zd\n",
  1948. __func__,
  1949. sizeof(struct snd_lsm_event_status_v3));
  1950. err = -EFAULT;
  1951. goto done;
  1952. }
  1953. if (u_event_status.payload_size >
  1954. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  1955. dev_err(rtd->dev,
  1956. "%s: payload_size %d is invalid, max allowed = %d\n",
  1957. __func__, u_event_status.payload_size,
  1958. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  1959. err = -EINVAL;
  1960. goto done;
  1961. }
  1962. u_pld_size = sizeof(struct snd_lsm_event_status_v3) +
  1963. u_event_status.payload_size;
  1964. event_status = kzalloc(u_pld_size, GFP_KERNEL);
  1965. if (!event_status) {
  1966. err = -ENOMEM;
  1967. goto done;
  1968. } else {
  1969. event_status->payload_size =
  1970. u_event_status.payload_size;
  1971. err = msm_cpe_lsm_ioctl_shared(substream,
  1972. cmd, event_status);
  1973. }
  1974. if (!err && copy_to_user(arg, event_status, u_pld_size)) {
  1975. dev_err(rtd->dev,
  1976. "%s: copy to user failed\n",
  1977. __func__);
  1978. kfree(event_status);
  1979. err = -EFAULT;
  1980. goto done;
  1981. }
  1982. msm_cpe_lsm_lab_start(substream, event_status->status);
  1983. msm_cpe_process_event_status_done(lsm_d);
  1984. kfree(event_status);
  1985. }
  1986. break;
  1987. case SNDRV_LSM_SET_PARAMS: {
  1988. struct snd_lsm_detection_params det_params;
  1989. if (session->is_topology_used) {
  1990. dev_err(rtd->dev,
  1991. "%s: %s: not supported if using topology\n",
  1992. __func__, "SNDRV_LSM_SET_PARAMS");
  1993. err = -EINVAL;
  1994. goto done;
  1995. }
  1996. if (copy_from_user(&det_params, (void *) arg,
  1997. sizeof(det_params))) {
  1998. dev_err(rtd->dev,
  1999. "%s: %s: copy_from_user failed, size = %zd\n",
  2000. __func__, "SNDRV_LSM_SET_PARAMS",
  2001. sizeof(det_params));
  2002. err = -EFAULT;
  2003. goto done;
  2004. }
  2005. err = msm_cpe_lsm_ioctl_shared(substream, cmd,
  2006. &det_params);
  2007. }
  2008. break;
  2009. case SNDRV_LSM_SET_MODULE_PARAMS: {
  2010. struct snd_lsm_module_params p_data;
  2011. size_t p_size;
  2012. u8 *params;
  2013. if (!session->is_topology_used) {
  2014. dev_err(rtd->dev,
  2015. "%s: %s: not supported if not using topology\n",
  2016. __func__, "SET_MODULE_PARAMS");
  2017. err = -EINVAL;
  2018. goto done;
  2019. }
  2020. if (!arg) {
  2021. dev_err(rtd->dev,
  2022. "%s: %s: No Param data to set\n",
  2023. __func__, "SET_MODULE_PARAMS");
  2024. err = -EINVAL;
  2025. goto done;
  2026. }
  2027. if (copy_from_user(&p_data, arg,
  2028. sizeof(p_data))) {
  2029. dev_err(rtd->dev,
  2030. "%s: %s: copy_from_user failed, size = %zd\n",
  2031. __func__, "p_data", sizeof(p_data));
  2032. err = -EFAULT;
  2033. goto done;
  2034. }
  2035. if (p_data.num_params > LSM_PARAMS_MAX) {
  2036. dev_err(rtd->dev,
  2037. "%s: %s: Invalid num_params %d\n",
  2038. __func__, "SET_MODULE_PARAMS",
  2039. p_data.num_params);
  2040. err = -EINVAL;
  2041. goto done;
  2042. }
  2043. p_size = p_data.num_params *
  2044. sizeof(struct lsm_params_info);
  2045. if (p_data.data_size != p_size) {
  2046. dev_err(rtd->dev,
  2047. "%s: %s: Invalid size %zd\n",
  2048. __func__, "SET_MODULE_PARAMS", p_size);
  2049. err = -EFAULT;
  2050. goto done;
  2051. }
  2052. params = kzalloc(p_size, GFP_KERNEL);
  2053. if (!params) {
  2054. err = -ENOMEM;
  2055. goto done;
  2056. }
  2057. if (copy_from_user(params, p_data.params,
  2058. p_data.data_size)) {
  2059. dev_err(rtd->dev,
  2060. "%s: %s: copy_from_user failed, size = %d\n",
  2061. __func__, "params", p_data.data_size);
  2062. kfree(params);
  2063. err = -EFAULT;
  2064. goto done;
  2065. }
  2066. err = msm_cpe_lsm_process_params(substream, &p_data, params);
  2067. if (err)
  2068. dev_err(rtd->dev,
  2069. "%s: %s: Failed to set params, err = %d\n",
  2070. __func__, "SET_MODULE_PARAMS", err);
  2071. kfree(params);
  2072. break;
  2073. }
  2074. default:
  2075. err = msm_cpe_lsm_ioctl_shared(substream, cmd, arg);
  2076. break;
  2077. }
  2078. done:
  2079. MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
  2080. "lsm_api_lock");
  2081. return err;
  2082. }
  2083. #ifdef CONFIG_COMPAT
  2084. struct snd_lsm_sound_model_v2_32 {
  2085. compat_uptr_t data;
  2086. compat_uptr_t confidence_level;
  2087. u32 data_size;
  2088. enum lsm_detection_mode detection_mode;
  2089. u8 num_confidence_levels;
  2090. bool detect_failure;
  2091. };
  2092. struct snd_lsm_detection_params_32 {
  2093. compat_uptr_t conf_level;
  2094. enum lsm_detection_mode detect_mode;
  2095. u8 num_confidence_levels;
  2096. bool detect_failure;
  2097. };
  2098. struct lsm_params_info_32 {
  2099. u32 module_id;
  2100. u32 param_id;
  2101. u32 param_size;
  2102. compat_uptr_t param_data;
  2103. uint32_t param_type;
  2104. };
  2105. struct snd_lsm_module_params_32 {
  2106. compat_uptr_t params;
  2107. u32 num_params;
  2108. u32 data_size;
  2109. };
  2110. enum {
  2111. SNDRV_LSM_REG_SND_MODEL_V2_32 =
  2112. _IOW('U', 0x07, struct snd_lsm_sound_model_v2_32),
  2113. SNDRV_LSM_SET_PARAMS32 =
  2114. _IOW('U', 0x0A, struct snd_lsm_detection_params_32),
  2115. SNDRV_LSM_SET_MODULE_PARAMS_32 =
  2116. _IOW('U', 0x0B, struct snd_lsm_module_params_32),
  2117. };
  2118. static int msm_cpe_lsm_ioctl_compat(struct snd_pcm_substream *substream,
  2119. unsigned int cmd, void *arg)
  2120. {
  2121. int err = 0;
  2122. struct snd_soc_pcm_runtime *rtd;
  2123. struct cpe_priv *cpe = NULL;
  2124. struct cpe_lsm_data *lsm_d = NULL;
  2125. struct cpe_lsm_session *session = NULL;
  2126. struct wcd_cpe_lsm_ops *lsm_ops;
  2127. if (!substream || !substream->private_data) {
  2128. pr_err("%s: invalid substream (%pK)\n",
  2129. __func__, substream);
  2130. return -EINVAL;
  2131. }
  2132. rtd = substream->private_data;
  2133. lsm_d = cpe_get_lsm_data(substream);
  2134. cpe = cpe_get_private_data(substream);
  2135. if (!cpe || !cpe->core_handle) {
  2136. dev_err(rtd->dev,
  2137. "%s: Invalid private data\n",
  2138. __func__);
  2139. return -EINVAL;
  2140. }
  2141. if (!lsm_d || !lsm_d->lsm_session) {
  2142. dev_err(rtd->dev,
  2143. "%s: Invalid session data\n",
  2144. __func__);
  2145. return -EINVAL;
  2146. }
  2147. MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
  2148. "lsm_api_lock");
  2149. session = lsm_d->lsm_session;
  2150. lsm_ops = &cpe->lsm_ops;
  2151. switch (cmd) {
  2152. case SNDRV_LSM_REG_SND_MODEL_V2_32: {
  2153. struct snd_lsm_sound_model_v2 snd_model;
  2154. struct snd_lsm_sound_model_v2_32 snd_model32;
  2155. if (session->is_topology_used) {
  2156. dev_err(rtd->dev,
  2157. "%s: %s: not supported if using topology\n",
  2158. __func__, "LSM_REG_SND_MODEL_V2_32");
  2159. err = -EINVAL;
  2160. goto done;
  2161. }
  2162. dev_dbg(rtd->dev,
  2163. "%s: ioctl %s\n", __func__,
  2164. "SNDRV_LSM_REG_SND_MODEL_V2_32");
  2165. if (copy_from_user(&snd_model32, (void *)arg,
  2166. sizeof(snd_model32))) {
  2167. dev_err(rtd->dev,
  2168. "%s: copy from user failed, size %zd\n",
  2169. __func__,
  2170. sizeof(snd_model32));
  2171. err = -EFAULT;
  2172. goto done;
  2173. }
  2174. snd_model.data = compat_ptr(snd_model32.data);
  2175. snd_model.confidence_level =
  2176. compat_ptr(snd_model32.confidence_level);
  2177. snd_model.data_size = snd_model32.data_size;
  2178. snd_model.detect_failure = snd_model32.detect_failure;
  2179. snd_model.num_confidence_levels =
  2180. snd_model32.num_confidence_levels;
  2181. snd_model.detection_mode = snd_model32.detection_mode;
  2182. cmd = SNDRV_LSM_REG_SND_MODEL_V2;
  2183. err = msm_cpe_lsm_ioctl_shared(substream, cmd, &snd_model);
  2184. if (err)
  2185. dev_err(rtd->dev,
  2186. "%s: %s failed, error = %d\n",
  2187. __func__,
  2188. "SNDRV_LSM_REG_SND_MODEL_V2_32",
  2189. err);
  2190. }
  2191. break;
  2192. case SNDRV_LSM_EVENT_STATUS: {
  2193. struct snd_lsm_event_status *event_status = NULL;
  2194. struct snd_lsm_event_status u_event_status32;
  2195. struct snd_lsm_event_status *udata_32 = NULL;
  2196. int u_pld_size = 0;
  2197. dev_dbg(rtd->dev,
  2198. "%s: ioctl %s\n", __func__,
  2199. "SNDRV_LSM_EVENT_STATUS32");
  2200. if (copy_from_user(&u_event_status32, (void *)arg,
  2201. sizeof(struct snd_lsm_event_status))) {
  2202. dev_err(rtd->dev,
  2203. "%s: event status copy from user failed, size %zd\n",
  2204. __func__,
  2205. sizeof(struct snd_lsm_event_status));
  2206. err = -EFAULT;
  2207. goto done;
  2208. }
  2209. if (u_event_status32.payload_size >
  2210. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  2211. dev_err(rtd->dev,
  2212. "%s: payload_size %d is invalid, max allowed = %d\n",
  2213. __func__, u_event_status32.payload_size,
  2214. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  2215. err = -EINVAL;
  2216. goto done;
  2217. }
  2218. u_pld_size = sizeof(struct snd_lsm_event_status) +
  2219. u_event_status32.payload_size;
  2220. event_status = kzalloc(u_pld_size, GFP_KERNEL);
  2221. if (!event_status) {
  2222. dev_err(rtd->dev,
  2223. "%s: No memory for event status\n",
  2224. __func__);
  2225. err = -ENOMEM;
  2226. goto done;
  2227. } else {
  2228. event_status->payload_size =
  2229. u_event_status32.payload_size;
  2230. err = msm_cpe_lsm_ioctl_shared(substream,
  2231. cmd, event_status);
  2232. if (err)
  2233. dev_err(rtd->dev,
  2234. "%s: %s failed, error = %d\n",
  2235. __func__,
  2236. "SNDRV_LSM_EVENT_STATUS32",
  2237. err);
  2238. }
  2239. if (!err) {
  2240. udata_32 = kzalloc(u_pld_size, GFP_KERNEL);
  2241. if (!udata_32) {
  2242. dev_err(rtd->dev,
  2243. "%s: nomem for udata\n",
  2244. __func__);
  2245. err = -EFAULT;
  2246. } else {
  2247. udata_32->status = event_status->status;
  2248. udata_32->payload_size =
  2249. event_status->payload_size;
  2250. memcpy(udata_32->payload,
  2251. event_status->payload,
  2252. u_pld_size);
  2253. }
  2254. }
  2255. if (!err && copy_to_user(arg, udata_32,
  2256. u_pld_size)) {
  2257. dev_err(rtd->dev,
  2258. "%s: copy to user failed\n",
  2259. __func__);
  2260. kfree(event_status);
  2261. kfree(udata_32);
  2262. err = -EFAULT;
  2263. goto done;
  2264. }
  2265. msm_cpe_lsm_lab_start(substream, event_status->status);
  2266. msm_cpe_process_event_status_done(lsm_d);
  2267. kfree(event_status);
  2268. kfree(udata_32);
  2269. }
  2270. break;
  2271. case SNDRV_LSM_EVENT_STATUS_V3: {
  2272. struct snd_lsm_event_status_v3 *event_status = NULL;
  2273. struct snd_lsm_event_status_v3 u_event_status32;
  2274. struct snd_lsm_event_status_v3 *udata_32 = NULL;
  2275. int u_pld_size = 0;
  2276. dev_dbg(rtd->dev,
  2277. "%s: ioctl %s\n", __func__,
  2278. "SNDRV_LSM_EVENT_STATUS_V3_32");
  2279. if (copy_from_user(&u_event_status32, (void *)arg,
  2280. sizeof(struct snd_lsm_event_status_v3))) {
  2281. dev_err(rtd->dev,
  2282. "%s: event status copy from user failed, size %zd\n",
  2283. __func__,
  2284. sizeof(struct snd_lsm_event_status_v3));
  2285. err = -EFAULT;
  2286. goto done;
  2287. }
  2288. if (u_event_status32.payload_size >
  2289. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  2290. dev_err(rtd->dev,
  2291. "%s: payload_size %d is invalid, max allowed = %d\n",
  2292. __func__, u_event_status32.payload_size,
  2293. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  2294. err = -EINVAL;
  2295. goto done;
  2296. }
  2297. u_pld_size = sizeof(struct snd_lsm_event_status_v3) +
  2298. u_event_status32.payload_size;
  2299. event_status = kzalloc(u_pld_size, GFP_KERNEL);
  2300. if (!event_status) {
  2301. dev_err(rtd->dev,
  2302. "%s: No memory for event status\n",
  2303. __func__);
  2304. err = -ENOMEM;
  2305. goto done;
  2306. } else {
  2307. event_status->payload_size =
  2308. u_event_status32.payload_size;
  2309. err = msm_cpe_lsm_ioctl_shared(substream,
  2310. cmd, event_status);
  2311. if (err)
  2312. dev_err(rtd->dev,
  2313. "%s: %s failed, error = %d\n",
  2314. __func__,
  2315. "SNDRV_LSM_EVENT_STATUS_V3_32",
  2316. err);
  2317. }
  2318. if (!err) {
  2319. udata_32 = kzalloc(u_pld_size, GFP_KERNEL);
  2320. if (!udata_32) {
  2321. dev_err(rtd->dev,
  2322. "%s: nomem for udata\n",
  2323. __func__);
  2324. err = -EFAULT;
  2325. } else {
  2326. udata_32->timestamp_lsw =
  2327. event_status->timestamp_lsw;
  2328. udata_32->timestamp_msw =
  2329. event_status->timestamp_msw;
  2330. udata_32->status = event_status->status;
  2331. udata_32->payload_size =
  2332. event_status->payload_size;
  2333. memcpy(udata_32->payload,
  2334. event_status->payload,
  2335. u_pld_size);
  2336. }
  2337. }
  2338. if (!err && copy_to_user(arg, udata_32,
  2339. u_pld_size)) {
  2340. dev_err(rtd->dev,
  2341. "%s: copy to user failed\n",
  2342. __func__);
  2343. kfree(event_status);
  2344. kfree(udata_32);
  2345. err = -EFAULT;
  2346. goto done;
  2347. }
  2348. msm_cpe_lsm_lab_start(substream, event_status->status);
  2349. msm_cpe_process_event_status_done(lsm_d);
  2350. kfree(event_status);
  2351. kfree(udata_32);
  2352. }
  2353. break;
  2354. case SNDRV_LSM_SET_PARAMS32: {
  2355. struct snd_lsm_detection_params_32 det_params32;
  2356. struct snd_lsm_detection_params det_params;
  2357. if (session->is_topology_used) {
  2358. dev_err(rtd->dev,
  2359. "%s: %s: not supported if using topology\n",
  2360. __func__, "SNDRV_LSM_SET_PARAMS32");
  2361. err = -EINVAL;
  2362. goto done;
  2363. }
  2364. if (copy_from_user(&det_params32, arg,
  2365. sizeof(det_params32))) {
  2366. err = -EFAULT;
  2367. dev_err(rtd->dev,
  2368. "%s: %s: copy_from_user failed, size = %zd\n",
  2369. __func__, "SNDRV_LSM_SET_PARAMS_32",
  2370. sizeof(det_params32));
  2371. } else {
  2372. det_params.conf_level =
  2373. compat_ptr(det_params32.conf_level);
  2374. det_params.detect_mode =
  2375. det_params32.detect_mode;
  2376. det_params.num_confidence_levels =
  2377. det_params32.num_confidence_levels;
  2378. det_params.detect_failure =
  2379. det_params32.detect_failure;
  2380. cmd = SNDRV_LSM_SET_PARAMS;
  2381. err = msm_cpe_lsm_ioctl_shared(substream, cmd,
  2382. &det_params);
  2383. if (err)
  2384. dev_err(rtd->dev,
  2385. "%s: ioctl %s failed\n", __func__,
  2386. "SNDRV_LSM_SET_PARAMS");
  2387. }
  2388. break;
  2389. }
  2390. case SNDRV_LSM_SET_MODULE_PARAMS_32: {
  2391. struct snd_lsm_module_params_32 p_data_32;
  2392. struct snd_lsm_module_params p_data;
  2393. u8 *params, *params32;
  2394. size_t p_size;
  2395. struct lsm_params_info_32 *p_info_32;
  2396. struct lsm_params_info *p_info;
  2397. int i;
  2398. if (!session->is_topology_used) {
  2399. dev_err(rtd->dev,
  2400. "%s: %s: not supported if not using topology\n",
  2401. __func__, "SET_MODULE_PARAMS_32");
  2402. err = -EINVAL;
  2403. goto done;
  2404. }
  2405. if (copy_from_user(&p_data_32, arg,
  2406. sizeof(p_data_32))) {
  2407. dev_err(rtd->dev,
  2408. "%s: %s: copy_from_user failed, size = %zd\n",
  2409. __func__, "SET_MODULE_PARAMS_32",
  2410. sizeof(p_data_32));
  2411. err = -EFAULT;
  2412. goto done;
  2413. }
  2414. p_data.params = compat_ptr(p_data_32.params);
  2415. p_data.num_params = p_data_32.num_params;
  2416. p_data.data_size = p_data_32.data_size;
  2417. if (p_data.num_params > LSM_PARAMS_MAX) {
  2418. dev_err(rtd->dev,
  2419. "%s: %s: Invalid num_params %d\n",
  2420. __func__, "SET_MODULE_PARAMS_32",
  2421. p_data.num_params);
  2422. err = -EINVAL;
  2423. goto done;
  2424. }
  2425. if (p_data.data_size !=
  2426. (p_data.num_params * sizeof(struct lsm_params_info_32))) {
  2427. dev_err(rtd->dev,
  2428. "%s: %s: Invalid size %d\n",
  2429. __func__, "SET_MODULE_PARAMS_32",
  2430. p_data.data_size);
  2431. err = -EINVAL;
  2432. goto done;
  2433. }
  2434. p_size = sizeof(struct lsm_params_info_32) *
  2435. p_data.num_params;
  2436. params32 = kzalloc(p_size, GFP_KERNEL);
  2437. if (!params32) {
  2438. err = -ENOMEM;
  2439. goto done;
  2440. }
  2441. p_size = sizeof(struct lsm_params_info) * p_data.num_params;
  2442. params = kzalloc(p_size, GFP_KERNEL);
  2443. if (!params) {
  2444. kfree(params32);
  2445. err = -ENOMEM;
  2446. goto done;
  2447. }
  2448. if (copy_from_user(params32, p_data.params,
  2449. p_data.data_size)) {
  2450. dev_err(rtd->dev,
  2451. "%s: %s: copy_from_user failed, size = %d\n",
  2452. __func__, "params32", p_data.data_size);
  2453. kfree(params32);
  2454. kfree(params);
  2455. err = -EFAULT;
  2456. goto done;
  2457. }
  2458. p_info_32 = (struct lsm_params_info_32 *) params32;
  2459. p_info = (struct lsm_params_info *) params;
  2460. for (i = 0; i < p_data.num_params; i++) {
  2461. p_info->module_id = p_info_32->module_id;
  2462. p_info->param_id = p_info_32->param_id;
  2463. p_info->param_size = p_info_32->param_size;
  2464. p_info->param_data = compat_ptr(p_info_32->param_data);
  2465. p_info->param_type = p_info_32->param_type;
  2466. p_info_32++;
  2467. p_info++;
  2468. }
  2469. err = msm_cpe_lsm_process_params(substream,
  2470. &p_data, params);
  2471. if (err)
  2472. dev_err(rtd->dev,
  2473. "%s: Failed to process params, err = %d\n",
  2474. __func__, err);
  2475. kfree(params);
  2476. kfree(params32);
  2477. break;
  2478. }
  2479. case SNDRV_LSM_REG_SND_MODEL_V2:
  2480. case SNDRV_LSM_SET_PARAMS:
  2481. case SNDRV_LSM_SET_MODULE_PARAMS:
  2482. /*
  2483. * In ideal cases, the compat_ioctl should never be called
  2484. * with the above unlocked ioctl commands. Print error
  2485. * and return error if it does.
  2486. */
  2487. dev_err(rtd->dev,
  2488. "%s: Invalid cmd for compat_ioctl\n",
  2489. __func__);
  2490. err = -EINVAL;
  2491. break;
  2492. default:
  2493. err = msm_cpe_lsm_ioctl_shared(substream, cmd, arg);
  2494. break;
  2495. }
  2496. done:
  2497. MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
  2498. "lsm_api_lock");
  2499. return err;
  2500. }
  2501. #else
  2502. #define msm_cpe_lsm_ioctl_compat NULL
  2503. #endif
  2504. /*
  2505. * msm_cpe_lsm_prepare: prepare call from ASoC core for this platform
  2506. * @substream: ASoC substream for which the operation is invoked
  2507. *
  2508. * start the AFE port on CPE associated for this listen session
  2509. */
  2510. static int msm_cpe_lsm_prepare(struct snd_pcm_substream *substream)
  2511. {
  2512. int rc = 0;
  2513. struct cpe_priv *cpe = cpe_get_private_data(substream);
  2514. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2515. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2516. struct wcd_cpe_afe_ops *afe_ops;
  2517. struct wcd_cpe_afe_port_cfg *afe_cfg;
  2518. struct cpe_lsm_session *lsm_session;
  2519. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  2520. struct snd_pcm_runtime *runtime = substream->runtime;
  2521. struct lsm_hw_params lsm_param;
  2522. struct wcd_cpe_lsm_ops *lsm_ops;
  2523. if (!cpe || !cpe->core_handle) {
  2524. dev_err(rtd->dev,
  2525. "%s: Invalid private data\n",
  2526. __func__);
  2527. return -EINVAL;
  2528. }
  2529. if (!lsm_d || !lsm_d->lsm_session) {
  2530. dev_err(rtd->dev,
  2531. "%s: Invalid session data\n",
  2532. __func__);
  2533. return -EINVAL;
  2534. }
  2535. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  2536. runtime->status->state == SNDRV_PCM_STATE_PREPARED) {
  2537. pr_err("%s: XRUN ignore for now\n", __func__);
  2538. return 0;
  2539. }
  2540. lsm_session = lsm_d->lsm_session;
  2541. lab_d->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  2542. dev_dbg(rtd->dev,
  2543. "%s: pcm_size 0x%x", __func__, lab_d->pcm_size);
  2544. if (lsm_d->cpe_prepared) {
  2545. dev_dbg(rtd->dev, "%s: CPE is alredy prepared\n",
  2546. __func__);
  2547. return 0;
  2548. }
  2549. lsm_ops = &cpe->lsm_ops;
  2550. afe_ops = &cpe->afe_ops;
  2551. afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
  2552. switch (cpe->input_port_id) {
  2553. case AFE_PORT_ID_3:
  2554. afe_cfg->port_id = AFE_PORT_ID_3;
  2555. afe_cfg->bit_width = 16;
  2556. afe_cfg->num_channels = 1;
  2557. afe_cfg->sample_rate = SAMPLE_RATE_48KHZ;
  2558. rc = afe_ops->afe_port_cmd_cfg(cpe->core_handle, afe_cfg);
  2559. break;
  2560. case AFE_PORT_ID_1:
  2561. default:
  2562. afe_cfg->port_id = AFE_PORT_ID_1;
  2563. afe_cfg->bit_width = 16;
  2564. afe_cfg->num_channels = 1;
  2565. afe_cfg->sample_rate = SAMPLE_RATE_16KHZ;
  2566. rc = afe_ops->afe_set_params(cpe->core_handle,
  2567. afe_cfg, cpe->afe_mad_ctl);
  2568. break;
  2569. }
  2570. if (rc != 0) {
  2571. dev_err(rtd->dev,
  2572. "%s: cpe afe params failed for port = %d, err = %d\n",
  2573. __func__, afe_cfg->port_id, rc);
  2574. return rc;
  2575. }
  2576. lsm_param.sample_rate = afe_cfg->sample_rate;
  2577. lsm_param.num_chs = afe_cfg->num_channels;
  2578. lsm_param.bit_width = afe_cfg->bit_width;
  2579. rc = lsm_ops->lsm_set_media_fmt_params(cpe->core_handle, lsm_session,
  2580. &lsm_param);
  2581. if (rc)
  2582. dev_dbg(rtd->dev,
  2583. "%s: failed to set lsm media fmt params, err = %d\n",
  2584. __func__, rc);
  2585. /* Send connect to port (input) */
  2586. rc = lsm_ops->lsm_set_port(cpe->core_handle, lsm_session,
  2587. &cpe->input_port_id);
  2588. if (rc) {
  2589. dev_err(rtd->dev,
  2590. "%s: Failed to set connect input port, err=%d\n",
  2591. __func__, rc);
  2592. return rc;
  2593. }
  2594. if (cpe->input_port_id != 3) {
  2595. rc = lsm_ops->lsm_get_afe_out_port_id(cpe->core_handle,
  2596. lsm_session);
  2597. if (rc != 0) {
  2598. dev_err(rtd->dev,
  2599. "%s: failed to get port id, err = %d\n",
  2600. __func__, rc);
  2601. return rc;
  2602. }
  2603. /* Send connect to port (output) */
  2604. rc = lsm_ops->lsm_set_port(cpe->core_handle, lsm_session,
  2605. &lsm_session->afe_out_port_id);
  2606. if (rc) {
  2607. dev_err(rtd->dev,
  2608. "%s: Failed to set connect output port, err=%d\n",
  2609. __func__, rc);
  2610. return rc;
  2611. }
  2612. }
  2613. rc = msm_cpe_afe_port_cntl(substream,
  2614. cpe->core_handle,
  2615. afe_ops, afe_cfg,
  2616. AFE_CMD_PORT_START);
  2617. if (rc)
  2618. dev_err(rtd->dev,
  2619. "%s: cpe_afe_port start failed, err = %d\n",
  2620. __func__, rc);
  2621. else
  2622. lsm_d->cpe_prepared = true;
  2623. return rc;
  2624. }
  2625. /*
  2626. * msm_cpe_lsm_trigger: trigger call from ASoC core for this platform
  2627. * @substream: ASoC substream for which the operation is invoked
  2628. * @cmd: the trigger command from framework
  2629. *
  2630. * suspend/resume the AFE port on CPE associated with listen session
  2631. */
  2632. static int msm_cpe_lsm_trigger(struct snd_pcm_substream *substream,
  2633. int cmd)
  2634. {
  2635. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2636. struct cpe_priv *cpe = cpe_get_private_data(substream);
  2637. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2638. struct wcd_cpe_afe_ops *afe_ops;
  2639. struct wcd_cpe_afe_port_cfg *afe_cfg;
  2640. int afe_cmd = AFE_CMD_INVALID;
  2641. int rc = 0;
  2642. if (!cpe || !cpe->core_handle) {
  2643. dev_err(rtd->dev,
  2644. "%s: Invalid private data\n",
  2645. __func__);
  2646. return -EINVAL;
  2647. }
  2648. if (!lsm_d || !lsm_d->lsm_session) {
  2649. dev_err(rtd->dev,
  2650. "%s: Invalid session data\n",
  2651. __func__);
  2652. return -EINVAL;
  2653. }
  2654. afe_ops = &cpe->afe_ops;
  2655. afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
  2656. switch (cmd) {
  2657. case SNDRV_PCM_TRIGGER_SUSPEND:
  2658. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  2659. afe_cmd = AFE_CMD_PORT_SUSPEND;
  2660. break;
  2661. case SNDRV_PCM_TRIGGER_RESUME:
  2662. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  2663. afe_cmd = AFE_CMD_PORT_RESUME;
  2664. break;
  2665. default:
  2666. afe_cmd = AFE_CMD_INVALID;
  2667. dev_dbg(rtd->dev,
  2668. "%s: unhandled trigger cmd %d\n",
  2669. __func__, cmd);
  2670. break;
  2671. }
  2672. if (afe_cmd != AFE_CMD_INVALID)
  2673. rc = msm_cpe_afe_port_cntl(substream,
  2674. cpe->core_handle,
  2675. afe_ops, afe_cfg,
  2676. afe_cmd);
  2677. return rc;
  2678. }
  2679. static int msm_cpe_lsm_hwparams(struct snd_pcm_substream *substream,
  2680. struct snd_pcm_hw_params *params)
  2681. {
  2682. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2683. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2684. struct cpe_priv *cpe = cpe_get_private_data(substream);
  2685. struct cpe_lsm_session *session = NULL;
  2686. struct cpe_hw_params *hw_params = NULL;
  2687. if (!cpe || !cpe->core_handle) {
  2688. dev_err(rtd->dev,
  2689. "%s: Invalid %s\n",
  2690. __func__,
  2691. (!cpe) ? "cpe" : "core");
  2692. return -EINVAL;
  2693. }
  2694. if (!lsm_d || !lsm_d->lsm_session) {
  2695. dev_err(rtd->dev,
  2696. "%s: Invalid %s\n",
  2697. __func__,
  2698. (!lsm_d) ? "priv_data" : "session");
  2699. return -EINVAL;
  2700. }
  2701. session = lsm_d->lsm_session;
  2702. hw_params = &lsm_d->hw_params;
  2703. hw_params->buf_sz = (params_buffer_bytes(params)
  2704. / params_periods(params));
  2705. hw_params->period_count = params_periods(params);
  2706. hw_params->channels = params_channels(params);
  2707. hw_params->sample_rate = params_rate(params);
  2708. if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE)
  2709. hw_params->sample_size = 16;
  2710. else if (params_format(params) ==
  2711. SNDRV_PCM_FORMAT_S24_LE)
  2712. hw_params->sample_size = 24;
  2713. else if (params_format(params) ==
  2714. SNDRV_PCM_FORMAT_S32_LE)
  2715. hw_params->sample_size = 32;
  2716. else {
  2717. dev_err(rtd->dev,
  2718. "%s: Invalid Format 0x%x\n",
  2719. __func__, params_format(params));
  2720. return -EINVAL;
  2721. }
  2722. dev_dbg(rtd->dev,
  2723. "%s: Format %d buffer size(bytes) %d period count %d\n"
  2724. " Channel %d period in bytes 0x%x Period Size 0x%x rate = %d\n",
  2725. __func__, params_format(params), params_buffer_bytes(params),
  2726. params_periods(params), params_channels(params),
  2727. params_period_bytes(params), params_period_size(params),
  2728. params_rate(params));
  2729. return 0;
  2730. }
  2731. static snd_pcm_uframes_t msm_cpe_lsm_pointer(
  2732. struct snd_pcm_substream *substream)
  2733. {
  2734. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2735. struct snd_pcm_runtime *runtime = substream->runtime;
  2736. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2737. struct cpe_lsm_session *session;
  2738. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  2739. session = lsm_d->lsm_session;
  2740. if (lab_d->dma_write >= lab_d->pcm_size)
  2741. lab_d->dma_write = 0;
  2742. dev_dbg(rtd->dev,
  2743. "%s:pcm_dma_pos = %d\n",
  2744. __func__, lab_d->dma_write);
  2745. return bytes_to_frames(runtime, (lab_d->dma_write));
  2746. }
  2747. static int msm_cpe_lsm_copy(struct snd_pcm_substream *substream, int a,
  2748. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  2749. {
  2750. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2751. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2752. struct snd_pcm_runtime *runtime = substream->runtime;
  2753. struct cpe_lsm_session *session;
  2754. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  2755. char *pcm_buf;
  2756. int rc = 0;
  2757. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  2758. runtime->status->state == SNDRV_PCM_STATE_PREPARED) {
  2759. pr_err("%s: XRUN ignore for now\n", __func__);
  2760. return 0;
  2761. }
  2762. session = lsm_d->lsm_session;
  2763. /* Check if buffer reading is already in error state */
  2764. if (lab_d->thread_status == MSM_LSM_LAB_THREAD_ERROR) {
  2765. dev_err(rtd->dev,
  2766. "%s: Bufferring is in error state\n",
  2767. __func__);
  2768. /*
  2769. * Advance the period so there is no wait in case
  2770. * read is invoked even after error is propogated
  2771. */
  2772. atomic_inc(&lab_d->in_count);
  2773. lab_d->dma_write += snd_pcm_lib_period_bytes(substream);
  2774. snd_pcm_period_elapsed(substream);
  2775. return -ENETRESET;
  2776. } else if (lab_d->thread_status == MSM_LSM_LAB_THREAD_STOP) {
  2777. dev_err(rtd->dev,
  2778. "%s: Buferring is in stopped\n",
  2779. __func__);
  2780. return -EIO;
  2781. }
  2782. rc = wait_event_timeout(lab_d->period_wait,
  2783. (atomic_read(&lab_d->in_count) ||
  2784. atomic_read(&lab_d->abort_read)),
  2785. (2 * HZ));
  2786. if (atomic_read(&lab_d->abort_read)) {
  2787. pr_debug("%s: LSM LAB Abort read\n", __func__);
  2788. return -EIO;
  2789. }
  2790. if (lab_d->thread_status != MSM_LSM_LAB_THREAD_RUNNING) {
  2791. pr_err("%s: Lab stopped\n", __func__);
  2792. return -EIO;
  2793. }
  2794. if (!rc) {
  2795. pr_err("%s:LAB err wait_event_timeout\n", __func__);
  2796. rc = -EAGAIN;
  2797. goto fail;
  2798. }
  2799. if (lab_d->buf_idx >= (lsm_d->hw_params.period_count))
  2800. lab_d->buf_idx = 0;
  2801. pcm_buf = (lab_d->pcm_buf[lab_d->buf_idx].mem);
  2802. pr_debug("%s: Buf IDX = 0x%x pcm_buf %pK\n",
  2803. __func__, lab_d->buf_idx, pcm_buf);
  2804. if (pcm_buf) {
  2805. if (copy_to_user(buf, pcm_buf, fbytes)) {
  2806. pr_err("Failed to copy buf to user\n");
  2807. rc = -EFAULT;
  2808. goto fail;
  2809. }
  2810. }
  2811. lab_d->buf_idx++;
  2812. atomic_dec(&lab_d->in_count);
  2813. return 0;
  2814. fail:
  2815. return rc;
  2816. }
  2817. /*
  2818. * msm_asoc_cpe_lsm_probe: ASoC framework for lsm platform driver
  2819. * @platform: platform registered with ASoC core
  2820. *
  2821. * Allocate the private data for this platform and obtain the ops for
  2822. * lsm and afe modules from underlying driver. Also find the codec
  2823. * for this platform as specified by machine driver for ASoC framework.
  2824. */
  2825. static int msm_asoc_cpe_lsm_probe(struct snd_soc_platform *platform)
  2826. {
  2827. struct snd_soc_card *card;
  2828. struct snd_soc_pcm_runtime *rtd;
  2829. struct snd_soc_codec *codec;
  2830. struct cpe_priv *cpe_priv;
  2831. const struct snd_kcontrol_new *kcontrol;
  2832. bool found_runtime = false;
  2833. const char *cpe_dev_id = "qcom,msm-cpe-lsm-id";
  2834. u32 port_id = 0;
  2835. int ret = 0;
  2836. if (!platform || !platform->component.card) {
  2837. pr_err("%s: Invalid platform or card\n",
  2838. __func__);
  2839. return -EINVAL;
  2840. }
  2841. card = platform->component.card;
  2842. /* Match platform to codec */
  2843. list_for_each_entry(rtd, &card->rtd_list, list) {
  2844. if (!rtd->platform)
  2845. continue;
  2846. if (!strcmp(rtd->platform->component.name,
  2847. platform->component.name)) {
  2848. found_runtime = true;
  2849. break;
  2850. }
  2851. }
  2852. if (!found_runtime) {
  2853. dev_err(platform->dev,
  2854. "%s: Failed to find runtime for platform\n",
  2855. __func__);
  2856. return -EINVAL;
  2857. }
  2858. ret = of_property_read_u32(platform->dev->of_node, cpe_dev_id,
  2859. &port_id);
  2860. if (ret) {
  2861. dev_dbg(platform->dev,
  2862. "%s: missing 0x%x in dt node\n", __func__, port_id);
  2863. port_id = 1;
  2864. }
  2865. codec = rtd->codec;
  2866. cpe_priv = kzalloc(sizeof(struct cpe_priv),
  2867. GFP_KERNEL);
  2868. if (!cpe_priv)
  2869. return -ENOMEM;
  2870. cpe_priv->codec = codec;
  2871. cpe_priv->input_port_id = port_id;
  2872. wcd_cpe_get_lsm_ops(&cpe_priv->lsm_ops);
  2873. wcd_cpe_get_afe_ops(&cpe_priv->afe_ops);
  2874. snd_soc_platform_set_drvdata(platform, cpe_priv);
  2875. kcontrol = &msm_cpe_kcontrols[0];
  2876. snd_ctl_add(card->snd_card, snd_ctl_new1(kcontrol, cpe_priv));
  2877. return 0;
  2878. }
  2879. static const struct snd_pcm_ops msm_cpe_lsm_ops = {
  2880. .open = msm_cpe_lsm_open,
  2881. .close = msm_cpe_lsm_close,
  2882. .ioctl = msm_cpe_lsm_ioctl,
  2883. .prepare = msm_cpe_lsm_prepare,
  2884. .trigger = msm_cpe_lsm_trigger,
  2885. .pointer = msm_cpe_lsm_pointer,
  2886. .copy_user = msm_cpe_lsm_copy,
  2887. .hw_params = msm_cpe_lsm_hwparams,
  2888. .compat_ioctl = msm_cpe_lsm_ioctl_compat,
  2889. };
  2890. static struct snd_soc_platform_driver msm_soc_cpe_platform = {
  2891. .ops = &msm_cpe_lsm_ops,
  2892. .probe = msm_asoc_cpe_lsm_probe,
  2893. };
  2894. /*
  2895. * msm_cpe_lsm_probe: platform driver probe
  2896. * @pdev: platform device
  2897. *
  2898. * Register the ASoC platform driver with ASoC core
  2899. */
  2900. static int msm_cpe_lsm_probe(struct platform_device *pdev)
  2901. {
  2902. return snd_soc_register_platform(&pdev->dev,
  2903. &msm_soc_cpe_platform);
  2904. }
  2905. /*
  2906. * msm_cpe_lsm_remove: platform driver remove
  2907. * @pdev: platform device
  2908. *
  2909. * Deregister the ASoC platform driver
  2910. */
  2911. static int msm_cpe_lsm_remove(struct platform_device *pdev)
  2912. {
  2913. snd_soc_unregister_platform(&pdev->dev);
  2914. return 0;
  2915. }
  2916. static const struct of_device_id msm_cpe_lsm_dt_match[] = {
  2917. {.compatible = "qcom,msm-cpe-lsm" },
  2918. { }
  2919. };
  2920. static struct platform_driver msm_cpe_lsm_driver = {
  2921. .driver = {
  2922. .name = "msm-cpe-lsm",
  2923. .owner = THIS_MODULE,
  2924. .of_match_table = of_match_ptr(msm_cpe_lsm_dt_match),
  2925. },
  2926. .probe = msm_cpe_lsm_probe,
  2927. .remove = msm_cpe_lsm_remove,
  2928. };
  2929. int __init msm_cpe_lsm_init(void)
  2930. {
  2931. return platform_driver_register(&msm_cpe_lsm_driver);
  2932. }
  2933. void __exit msm_cpe_lsm_exit(void)
  2934. {
  2935. platform_driver_unregister(&msm_cpe_lsm_driver);
  2936. }
  2937. module_init(msm_cpe_lsm_init);
  2938. module_exit(msm_cpe_lsm_exit);
  2939. MODULE_DESCRIPTION("CPE LSM platform driver");
  2940. MODULE_DEVICE_TABLE(of, msm_cpe_lsm_dt_match);
  2941. MODULE_LICENSE("GPL v2");