msm-cpe-lsm.c 81 KB

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