msm-lsm-client.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/err.h>
  7. #include <linux/module.h>
  8. #include <linux/time.h>
  9. #include <linux/wait.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/of.h>
  14. #include <linux/freezer.h>
  15. #include <sound/core.h>
  16. #include <sound/soc.h>
  17. #include <sound/soc-dapm.h>
  18. #include <sound/pcm.h>
  19. #include <sound/timer.h>
  20. #include <sound/initval.h>
  21. #include <sound/control.h>
  22. #include <audio/sound/lsm_params.h>
  23. #include <sound/pcm_params.h>
  24. #include <dsp/msm_audio_ion.h>
  25. #include <dsp/q6lsm.h>
  26. #include "msm-pcm-routing-v2.h"
  27. #define DRV_NAME "msm-lsm-client"
  28. #define CAPTURE_MIN_NUM_PERIODS 2
  29. #define CAPTURE_MAX_NUM_PERIODS 8
  30. #define CAPTURE_MAX_PERIOD_SIZE 61440
  31. #define CAPTURE_MIN_PERIOD_SIZE 320
  32. #define LISTEN_MAX_STATUS_PAYLOAD_SIZE 256
  33. #define WAKELOCK_TIMEOUT 2000
  34. #define LAB_BUFFER_ALLOC 1
  35. #define LAB_BUFFER_DEALLOC 0
  36. #define LSM_IS_LAST_STAGE(client, stage_idx) \
  37. (client->num_stages == (stage_idx + 1))
  38. static struct snd_pcm_hardware msm_pcm_hardware_capture = {
  39. .info = (SNDRV_PCM_INFO_MMAP |
  40. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  41. SNDRV_PCM_INFO_INTERLEAVED |
  42. SNDRV_PCM_INFO_PAUSE | SNDRV_PCM_INFO_RESUME),
  43. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  44. SNDRV_PCM_FMTBIT_S24_LE),
  45. .rates = (SNDRV_PCM_RATE_16000 |
  46. SNDRV_PCM_RATE_48000),
  47. .rate_min = 16000,
  48. .rate_max = 48000,
  49. .channels_min = LSM_INPUT_NUM_CHANNELS_MIN,
  50. .channels_max = LSM_INPUT_NUM_CHANNELS_MAX,
  51. .buffer_bytes_max = CAPTURE_MAX_NUM_PERIODS *
  52. CAPTURE_MAX_PERIOD_SIZE,
  53. .period_bytes_min = CAPTURE_MIN_PERIOD_SIZE,
  54. .period_bytes_max = CAPTURE_MAX_PERIOD_SIZE,
  55. .periods_min = CAPTURE_MIN_NUM_PERIODS,
  56. .periods_max = CAPTURE_MAX_NUM_PERIODS,
  57. .fifo_size = 0,
  58. };
  59. /* Conventional and unconventional sample rate supported */
  60. static unsigned int supported_sample_rates[] = {
  61. 16000, 48000,
  62. };
  63. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  64. .count = ARRAY_SIZE(supported_sample_rates),
  65. .list = supported_sample_rates,
  66. .mask = 0,
  67. };
  68. struct lsm_priv {
  69. struct snd_pcm_substream *substream;
  70. struct lsm_client *lsm_client;
  71. struct snd_lsm_event_status_v3 *event_status;
  72. struct snd_lsm_event_status *det_event;
  73. spinlock_t event_lock;
  74. wait_queue_head_t event_wait;
  75. unsigned long event_avail;
  76. atomic_t event_wait_stop;
  77. atomic_t buf_count;
  78. atomic_t read_abort;
  79. wait_queue_head_t period_wait;
  80. struct mutex lsm_api_lock;
  81. int appl_cnt;
  82. int dma_write;
  83. int xrun_count;
  84. int xrun_index;
  85. spinlock_t xrun_lock;
  86. struct wakeup_source *ws;
  87. };
  88. enum { /* lsm session states */
  89. IDLE = 0,
  90. RUNNING,
  91. };
  92. static int msm_lsm_queue_lab_buffer(struct lsm_priv *prtd, int i)
  93. {
  94. int rc = 0;
  95. struct lsm_cmd_read cmd_read;
  96. struct snd_soc_pcm_runtime *rtd;
  97. if (!prtd || !prtd->lsm_client) {
  98. pr_err("%s: Invalid params prtd %pK lsm client %pK\n",
  99. __func__, prtd, ((!prtd) ? NULL : prtd->lsm_client));
  100. return -EINVAL;
  101. }
  102. if (!prtd->substream || !prtd->substream->private_data) {
  103. pr_err("%s: Invalid %s\n", __func__,
  104. (!prtd->substream) ? "substream" : "private_data");
  105. return -EINVAL;
  106. }
  107. rtd = prtd->substream->private_data;
  108. if (!prtd->lsm_client->lab_buffer ||
  109. i >= prtd->lsm_client->out_hw_params.period_count) {
  110. dev_err(rtd->dev,
  111. "%s: Lab buffer not setup %pK incorrect index %d period count %d\n",
  112. __func__, prtd->lsm_client->lab_buffer, i,
  113. prtd->lsm_client->out_hw_params.period_count);
  114. return -EINVAL;
  115. }
  116. cmd_read.buf_addr_lsw =
  117. lower_32_bits(prtd->lsm_client->lab_buffer[i].phys);
  118. cmd_read.buf_addr_msw =
  119. msm_audio_populate_upper_32_bits(
  120. prtd->lsm_client->lab_buffer[i].phys);
  121. cmd_read.buf_size = prtd->lsm_client->lab_buffer[i].size;
  122. cmd_read.mem_map_handle =
  123. prtd->lsm_client->lab_buffer[i].mem_map_handle;
  124. rc = q6lsm_read(prtd->lsm_client, &cmd_read);
  125. if (rc)
  126. dev_err(rtd->dev,
  127. "%s: error in queuing the lab buffer rc %d\n",
  128. __func__, rc);
  129. return rc;
  130. }
  131. static int lsm_lab_buffer_sanity(struct lsm_priv *prtd,
  132. struct lsm_cmd_read_done *read_done, int *index)
  133. {
  134. int i = 0, rc = -EINVAL;
  135. struct snd_soc_pcm_runtime *rtd;
  136. if (!prtd || !read_done || !index) {
  137. pr_err("%s: Invalid params prtd %pK read_done %pK index %pK\n",
  138. __func__, prtd, read_done, index);
  139. return -EINVAL;
  140. }
  141. if (!prtd->substream || !prtd->substream->private_data) {
  142. pr_err("%s: Invalid %s\n", __func__,
  143. (!prtd->substream) ? "substream" : "private_data");
  144. return -EINVAL;
  145. }
  146. rtd = prtd->substream->private_data;
  147. if (!prtd->lsm_client->lab_enable || !prtd->lsm_client->lab_buffer) {
  148. dev_err(rtd->dev,
  149. "%s: Lab not enabled %d invalid lab buffer %pK\n",
  150. __func__, prtd->lsm_client->lab_enable,
  151. prtd->lsm_client->lab_buffer);
  152. return -EINVAL;
  153. }
  154. for (i = 0; i < prtd->lsm_client->out_hw_params.period_count; i++) {
  155. if ((lower_32_bits(prtd->lsm_client->lab_buffer[i].phys) ==
  156. read_done->buf_addr_lsw) &&
  157. (msm_audio_populate_upper_32_bits
  158. (prtd->lsm_client->lab_buffer[i].phys) ==
  159. read_done->buf_addr_msw) &&
  160. (prtd->lsm_client->lab_buffer[i].mem_map_handle ==
  161. read_done->mem_map_handle)) {
  162. dev_dbg(rtd->dev,
  163. "%s: Buffer found %pK memmap handle %d\n",
  164. __func__, &prtd->lsm_client->lab_buffer[i].phys,
  165. prtd->lsm_client->lab_buffer[i].mem_map_handle);
  166. if (read_done->total_size >
  167. prtd->lsm_client->lab_buffer[i].size) {
  168. dev_err(rtd->dev,
  169. "%s: Size mismatch call back size %d actual size %zd\n",
  170. __func__, read_done->total_size,
  171. prtd->lsm_client->lab_buffer[i].size);
  172. rc = -EINVAL;
  173. break;
  174. } else {
  175. *index = i;
  176. rc = 0;
  177. break;
  178. }
  179. }
  180. }
  181. return rc;
  182. }
  183. static void lsm_event_handler(uint32_t opcode, uint32_t token,
  184. uint32_t *payload, uint16_t client_size,
  185. void *priv)
  186. {
  187. unsigned long flags;
  188. struct lsm_priv *prtd = priv;
  189. struct snd_pcm_substream *substream = prtd->substream;
  190. struct snd_soc_pcm_runtime *rtd;
  191. struct snd_lsm_event_status_v3 *temp;
  192. uint16_t status = 0;
  193. uint16_t payload_size = 0;
  194. uint16_t index = 0;
  195. uint32_t event_ts_lsw = 0;
  196. uint32_t event_ts_msw = 0;
  197. if (!substream || !substream->private_data) {
  198. pr_err("%s: Invalid %s\n", __func__,
  199. (!substream) ? "substream" : "private_data");
  200. return;
  201. }
  202. rtd = substream->private_data;
  203. pm_wakeup_ws_event(prtd->ws, WAKELOCK_TIMEOUT, true);
  204. dev_dbg(rtd->dev, "%s: opcode %x\n", __func__, opcode);
  205. switch (opcode) {
  206. case LSM_DATA_EVENT_READ_DONE: {
  207. int rc;
  208. struct lsm_cmd_read_done *read_done = (struct lsm_cmd_read_done *)payload;
  209. int buf_index = 0;
  210. unsigned long flags = 0;
  211. if (prtd->lsm_client->session != token ||
  212. !read_done) {
  213. dev_err(rtd->dev,
  214. "%s: EVENT_READ_DONE invalid callback, session %d callback %d payload %pK",
  215. __func__, prtd->lsm_client->session,
  216. token, read_done);
  217. __pm_relax(prtd->ws);
  218. return;
  219. }
  220. if (atomic_read(&prtd->read_abort)) {
  221. dev_dbg(rtd->dev,
  222. "%s: read abort set skip data\n", __func__);
  223. __pm_relax(prtd->ws);
  224. return;
  225. }
  226. if (!lsm_lab_buffer_sanity(prtd, read_done, &buf_index)) {
  227. dev_dbg(rtd->dev,
  228. "%s: process read done index %d\n",
  229. __func__, buf_index);
  230. if (buf_index >=
  231. prtd->lsm_client->out_hw_params.period_count) {
  232. dev_err(rtd->dev,
  233. "%s: Invalid index %d buf_index max cnt %d\n",
  234. __func__, buf_index,
  235. prtd->lsm_client->out_hw_params.period_count);
  236. __pm_relax(prtd->ws);
  237. return;
  238. }
  239. spin_lock_irqsave(&prtd->xrun_lock, flags);
  240. prtd->dma_write += read_done->total_size;
  241. atomic_inc(&prtd->buf_count);
  242. snd_pcm_period_elapsed(substream);
  243. wake_up(&prtd->period_wait);
  244. if (atomic_read(&prtd->buf_count) <
  245. prtd->lsm_client->out_hw_params.period_count) {
  246. /* queue the next period buffer */
  247. buf_index = (buf_index + 1) %
  248. prtd->lsm_client->out_hw_params.period_count;
  249. rc = msm_lsm_queue_lab_buffer(prtd, buf_index);
  250. if (rc)
  251. dev_err(rtd->dev,
  252. "%s: error in queuing the lab buffer rc %d\n",
  253. __func__, rc);
  254. } else {
  255. dev_dbg(rtd->dev,
  256. "%s: xrun: further lab to be queued after read from user\n",
  257. __func__);
  258. if (!prtd->xrun_count)
  259. prtd->xrun_index = buf_index;
  260. (prtd->xrun_count)++;
  261. }
  262. spin_unlock_irqrestore(&prtd->xrun_lock, flags);
  263. } else
  264. dev_err(rtd->dev, "%s: Invalid lab buffer returned by dsp\n",
  265. __func__);
  266. break;
  267. }
  268. case LSM_SESSION_EVENT_DETECTION_STATUS:
  269. if (client_size < 3 * sizeof(uint8_t)) {
  270. dev_err(rtd->dev,
  271. "%s: client_size has invalid size[%d]\n",
  272. __func__, client_size);
  273. __pm_relax(prtd->ws);
  274. return;
  275. }
  276. status = (uint16_t)((uint8_t *)payload)[0];
  277. payload_size = (uint16_t)((uint8_t *)payload)[2];
  278. index = 4;
  279. dev_dbg(rtd->dev,
  280. "%s: event detect status = %d payload size = %d\n",
  281. __func__, status, payload_size);
  282. break;
  283. case LSM_SESSION_EVENT_DETECTION_STATUS_V2:
  284. if (client_size < 2 * sizeof(uint8_t)) {
  285. dev_err(rtd->dev,
  286. "%s: client_size has invalid size[%d]\n",
  287. __func__, client_size);
  288. __pm_relax(prtd->ws);
  289. return;
  290. }
  291. status = (uint16_t)((uint8_t *)payload)[0];
  292. payload_size = (uint16_t)((uint8_t *)payload)[1];
  293. index = 2;
  294. dev_dbg(rtd->dev,
  295. "%s: event detect status_v2 = %d payload size = %d\n",
  296. __func__, status, payload_size);
  297. break;
  298. case LSM_SESSION_EVENT_DETECTION_STATUS_V3:
  299. if (client_size < 2 * (sizeof(uint32_t) + sizeof(uint8_t))) {
  300. dev_err(rtd->dev,
  301. "%s: client_size has invalid size[%d]\n",
  302. __func__, client_size);
  303. __pm_relax(prtd->ws);
  304. return;
  305. }
  306. event_ts_lsw = ((uint32_t *)payload)[0];
  307. event_ts_msw = ((uint32_t *)payload)[1];
  308. status = (uint16_t)((uint8_t *)payload)[8];
  309. payload_size = (uint16_t)((uint8_t *)payload)[9];
  310. index = 10;
  311. dev_dbg(rtd->dev,
  312. "%s: ts_msw = %u, ts_lsw = %u, event detect status = %d payload size = %d\n",
  313. __func__, event_ts_msw, event_ts_lsw, status,
  314. payload_size);
  315. break;
  316. case LSM_SESSION_DETECTION_ENGINE_GENERIC_EVENT: {
  317. struct snd_lsm_event_status *tmp;
  318. if (client_size < 2 * sizeof(uint16_t)) {
  319. dev_err(rtd->dev,
  320. "%s: client_size has invalid size[%d]\n",
  321. __func__, client_size);
  322. __pm_relax(prtd->ws);
  323. return;
  324. }
  325. status = ((uint16_t *)payload)[0];
  326. payload_size = ((uint16_t *)payload)[1];
  327. spin_lock_irqsave(&prtd->event_lock, flags);
  328. tmp = krealloc(prtd->det_event,
  329. sizeof(struct snd_lsm_event_status) +
  330. payload_size, GFP_ATOMIC);
  331. if (!tmp) {
  332. spin_unlock_irqrestore(&prtd->event_lock, flags);
  333. dev_err(rtd->dev,
  334. "%s: Failed to allocate memory for %s, size = %zu\n",
  335. __func__,
  336. "LSM_SESSION_DETECTION_ENGINE_GENERIC_EVENT",
  337. sizeof(struct snd_lsm_event_status) +
  338. payload_size);
  339. __pm_relax(prtd->ws);
  340. return;
  341. }
  342. prtd->det_event = tmp;
  343. prtd->det_event->status = status;
  344. prtd->det_event->payload_size = payload_size;
  345. if (client_size >= payload_size + 4) {
  346. memcpy(prtd->det_event->payload,
  347. &((uint8_t *)payload)[4], payload_size);
  348. } else {
  349. spin_unlock_irqrestore(&prtd->event_lock, flags);
  350. dev_err(rtd->dev,
  351. "%s: Failed to copy memory with invalid size = %d\n",
  352. __func__, payload_size);
  353. __pm_relax(prtd->ws);
  354. return;
  355. }
  356. prtd->event_avail = 1;
  357. spin_unlock_irqrestore(&prtd->event_lock, flags);
  358. wake_up(&prtd->event_wait);
  359. if (substream->timer_running)
  360. snd_timer_interrupt(substream->timer, 1);
  361. dev_dbg(rtd->dev,
  362. "%s: Generic det event status = %d payload size = %d\n",
  363. __func__, prtd->det_event->status,
  364. prtd->det_event->payload_size);
  365. break;
  366. }
  367. default:
  368. break;
  369. }
  370. if (opcode == LSM_SESSION_EVENT_DETECTION_STATUS ||
  371. opcode == LSM_SESSION_EVENT_DETECTION_STATUS_V2 ||
  372. opcode == LSM_SESSION_EVENT_DETECTION_STATUS_V3) {
  373. spin_lock_irqsave(&prtd->event_lock, flags);
  374. dev_dbg(rtd->dev, "%s: detection status\n", __func__);
  375. temp = krealloc(prtd->event_status,
  376. sizeof(struct snd_lsm_event_status_v3) +
  377. payload_size, GFP_ATOMIC);
  378. if (!temp) {
  379. dev_err(rtd->dev, "%s: no memory for event status\n",
  380. __func__);
  381. __pm_relax(prtd->ws);
  382. return;
  383. }
  384. /*
  385. * event status timestamp will be non-zero and valid if
  386. * opcode is LSM_SESSION_EVENT_DETECTION_STATUS_V3
  387. */
  388. prtd->event_status = temp;
  389. prtd->event_status->timestamp_lsw = event_ts_lsw;
  390. prtd->event_status->timestamp_msw = event_ts_msw;
  391. prtd->event_status->status = status;
  392. prtd->event_status->payload_size = payload_size;
  393. if (likely(prtd->event_status)) {
  394. if (client_size >= (payload_size + index)) {
  395. memcpy(prtd->event_status->payload,
  396. &((uint8_t *)payload)[index],
  397. payload_size);
  398. prtd->event_avail = 1;
  399. spin_unlock_irqrestore(&prtd->event_lock, flags);
  400. dev_dbg(rtd->dev, "%s: wakeup event_wait\n", __func__);
  401. wake_up(&prtd->event_wait);
  402. } else {
  403. spin_unlock_irqrestore(&prtd->event_lock, flags);
  404. dev_err(rtd->dev,
  405. "%s: Failed to copy memory with invalid size = %d\n",
  406. __func__, payload_size);
  407. __pm_relax(prtd->ws);
  408. return;
  409. }
  410. } else {
  411. spin_unlock_irqrestore(&prtd->event_lock, flags);
  412. dev_err(rtd->dev,
  413. "%s: Couldn't allocate %d bytes of memory\n",
  414. __func__, payload_size);
  415. }
  416. if (substream->timer_running)
  417. snd_timer_interrupt(substream->timer, 1);
  418. }
  419. dev_dbg(rtd->dev, "%s: leave\n", __func__);
  420. }
  421. static int msm_lsm_lab_buffer_alloc(struct lsm_priv *lsm, int alloc)
  422. {
  423. int ret = 0;
  424. struct snd_dma_buffer *dma_buf = NULL;
  425. if (!lsm) {
  426. pr_err("%s: Invalid param lsm %pK\n", __func__, lsm);
  427. return -EINVAL;
  428. }
  429. if (alloc) {
  430. if (!lsm->substream) {
  431. pr_err("%s: substream is NULL\n", __func__);
  432. return -EINVAL;
  433. }
  434. ret = q6lsm_lab_buffer_alloc(lsm->lsm_client, alloc);
  435. if (ret) {
  436. pr_err("%s: alloc lab buffer failed ret %d\n",
  437. __func__, ret);
  438. goto exit;
  439. }
  440. dma_buf = &lsm->substream->dma_buffer;
  441. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  442. dma_buf->dev.dev = lsm->substream->pcm->card->dev;
  443. dma_buf->private_data = NULL;
  444. dma_buf->area = lsm->lsm_client->lab_buffer[0].data;
  445. dma_buf->addr = lsm->lsm_client->lab_buffer[0].phys;
  446. dma_buf->bytes = lsm->lsm_client->out_hw_params.buf_sz *
  447. lsm->lsm_client->out_hw_params.period_count;
  448. snd_pcm_set_runtime_buffer(lsm->substream, dma_buf);
  449. } else {
  450. ret = q6lsm_lab_buffer_alloc(lsm->lsm_client, alloc);
  451. if (ret)
  452. pr_err("%s: free lab buffer failed ret %d\n",
  453. __func__, ret);
  454. kfree(lsm->lsm_client->lab_buffer);
  455. lsm->lsm_client->lab_buffer = NULL;
  456. }
  457. exit:
  458. return ret;
  459. }
  460. static int msm_lsm_get_conf_levels(struct lsm_client *client,
  461. u8 *conf_levels_ptr)
  462. {
  463. int rc = 0;
  464. if (client->num_sound_models != 0) {
  465. if (client->num_keywords == 0) {
  466. pr_debug("%s: no number of confidence_values provided\n",
  467. __func__);
  468. client->multi_snd_model_confidence_levels = NULL;
  469. goto done;
  470. }
  471. client->multi_snd_model_confidence_levels =
  472. kzalloc((sizeof(uint32_t) * client->num_keywords),
  473. GFP_KERNEL);
  474. if (!client->multi_snd_model_confidence_levels) {
  475. pr_err("%s: No memory for confidence\n"
  476. "levels num of level from user = %d\n",
  477. __func__, client->num_keywords);
  478. rc = -ENOMEM;
  479. goto done;
  480. }
  481. if (copy_from_user((u8 *)client->multi_snd_model_confidence_levels,
  482. conf_levels_ptr,
  483. sizeof(uint32_t) * client->num_keywords)) {
  484. pr_err("%s: copy from user failed, number of keywords = %d\n",
  485. __func__, client->num_keywords);
  486. rc = -EFAULT;
  487. goto copy_err;
  488. }
  489. } else {
  490. if (client->num_confidence_levels == 0) {
  491. pr_debug("%s: no confidence levels provided\n",
  492. __func__);
  493. client->confidence_levels = NULL;
  494. goto done;
  495. }
  496. client->confidence_levels =
  497. kzalloc((sizeof(uint8_t) * client->num_confidence_levels),
  498. GFP_KERNEL);
  499. if (!client->confidence_levels) {
  500. pr_err("%s: No memory for confidence\n"
  501. "levels num of level from user = %d\n",
  502. __func__, client->num_confidence_levels);
  503. rc = -ENOMEM;
  504. goto done;
  505. }
  506. if (copy_from_user(client->confidence_levels,
  507. conf_levels_ptr,
  508. client->num_confidence_levels)) {
  509. pr_err("%s: copy from user failed, size = %d\n",
  510. __func__, client->num_confidence_levels);
  511. rc = -EFAULT;
  512. goto copy_err;
  513. }
  514. }
  515. return rc;
  516. copy_err:
  517. if (client->num_sound_models != 0) {
  518. kfree(client->multi_snd_model_confidence_levels);
  519. client->multi_snd_model_confidence_levels = NULL;
  520. } else {
  521. kfree(client->confidence_levels);
  522. client->confidence_levels = NULL;
  523. }
  524. done:
  525. return rc;
  526. }
  527. static int msm_lsm_set_epd(struct snd_pcm_substream *substream,
  528. struct lsm_params_info_v2 *p_info)
  529. {
  530. struct snd_pcm_runtime *runtime = substream->runtime;
  531. struct lsm_priv *prtd = runtime->private_data;
  532. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  533. int rc = 0;
  534. struct snd_lsm_ep_det_thres epd_th;
  535. if (p_info->param_size != sizeof(epd_th)) {
  536. dev_err(rtd->dev,
  537. "%s: Invalid param_size %d\n",
  538. __func__, p_info->param_size);
  539. rc = -EINVAL;
  540. goto done;
  541. }
  542. if (copy_from_user(&epd_th, p_info->param_data,
  543. p_info->param_size)) {
  544. dev_err(rtd->dev,
  545. "%s: copy_from_user failed, size = %d\n",
  546. __func__, p_info->param_size);
  547. rc = -EFAULT;
  548. goto done;
  549. }
  550. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  551. &epd_th, LSM_ENDPOINT_DETECT_THRESHOLD);
  552. if (rc)
  553. dev_err(rtd->dev,
  554. "%s: Failed to set epd param, err = %d\n",
  555. __func__, rc);
  556. done:
  557. return rc;
  558. }
  559. static int msm_lsm_set_mode(struct snd_pcm_substream *substream,
  560. struct lsm_params_info_v2 *p_info)
  561. {
  562. struct snd_pcm_runtime *runtime = substream->runtime;
  563. struct lsm_priv *prtd = runtime->private_data;
  564. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  565. struct snd_lsm_detect_mode mode;
  566. int rc = 0;
  567. if (p_info->param_size != sizeof(mode)) {
  568. dev_err(rtd->dev,
  569. "%s: Invalid param_size %d\n",
  570. __func__, p_info->param_size);
  571. rc = -EINVAL;
  572. goto done;
  573. }
  574. if (copy_from_user(&mode, p_info->param_data,
  575. sizeof(mode))) {
  576. dev_err(rtd->dev,
  577. "%s: copy_from_user failed, size = %zd\n",
  578. __func__, sizeof(mode));
  579. rc = -EFAULT;
  580. goto done;
  581. }
  582. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  583. &mode, LSM_OPERATION_MODE);
  584. if (rc)
  585. dev_err(rtd->dev,
  586. "%s: Failed to set det_mode param, err = %d\n",
  587. __func__, rc);
  588. done:
  589. return rc;
  590. }
  591. static int msm_lsm_set_gain(struct snd_pcm_substream *substream,
  592. struct lsm_params_info_v2 *p_info)
  593. {
  594. struct snd_pcm_runtime *runtime = substream->runtime;
  595. struct lsm_priv *prtd = runtime->private_data;
  596. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  597. struct snd_lsm_gain gain;
  598. int rc = 0;
  599. if (p_info->param_size != sizeof(gain)) {
  600. dev_err(rtd->dev,
  601. "%s: Invalid param_size %d\n",
  602. __func__, p_info->param_size);
  603. rc = -EINVAL;
  604. goto done;
  605. }
  606. if (copy_from_user(&gain, p_info->param_data,
  607. sizeof(gain))) {
  608. dev_err(rtd->dev,
  609. "%s: copy_from_user failed, size = %zd\n",
  610. __func__, sizeof(gain));
  611. rc = -EFAULT;
  612. goto done;
  613. }
  614. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  615. &gain, LSM_GAIN);
  616. if (rc)
  617. dev_err(rtd->dev,
  618. "%s: Failed to set det_mode param, err = %d\n",
  619. __func__, rc);
  620. done:
  621. return rc;
  622. }
  623. static int msm_lsm_set_conf(struct snd_pcm_substream *substream,
  624. struct lsm_params_info_v2 *p_info)
  625. {
  626. struct snd_pcm_runtime *runtime = substream->runtime;
  627. struct lsm_priv *prtd = runtime->private_data;
  628. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  629. int rc = 0;
  630. if (p_info->param_type == LSM_MULTI_SND_MODEL_CONFIDENCE_LEVELS) {
  631. if (p_info->param_size > MAX_KEYWORDS_SUPPORTED) {
  632. dev_err(rtd->dev,
  633. "%s: invalid number of snd_model keywords %d, the max is %d\n",
  634. __func__, p_info->param_size, MAX_KEYWORDS_SUPPORTED);
  635. return -EINVAL;
  636. }
  637. prtd->lsm_client->num_keywords = p_info->param_size;
  638. rc = msm_lsm_get_conf_levels(prtd->lsm_client,
  639. p_info->param_data);
  640. if (rc) {
  641. dev_err(rtd->dev,
  642. "%s: get_conf_levels failed for snd_model %d, err = %d\n",
  643. __func__, p_info->model_id, rc);
  644. return rc;
  645. }
  646. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  647. prtd->lsm_client->multi_snd_model_confidence_levels,
  648. LSM_MULTI_SND_MODEL_CONFIDENCE_LEVELS);
  649. if (rc)
  650. dev_err(rtd->dev,
  651. "%s: Failed to set multi_snd_model_confidence_levels, err = %d\n",
  652. __func__, rc);
  653. if (prtd->lsm_client->multi_snd_model_confidence_levels) {
  654. kfree(prtd->lsm_client->multi_snd_model_confidence_levels);
  655. prtd->lsm_client->multi_snd_model_confidence_levels = NULL;
  656. }
  657. } else {
  658. if (p_info->param_size > MAX_NUM_CONFIDENCE) {
  659. dev_err(rtd->dev,
  660. "%s: invalid confidence levels %d\n",
  661. __func__, p_info->param_size);
  662. return -EINVAL;
  663. }
  664. prtd->lsm_client->num_confidence_levels =
  665. p_info->param_size;
  666. rc = msm_lsm_get_conf_levels(prtd->lsm_client,
  667. p_info->param_data);
  668. if (rc) {
  669. dev_err(rtd->dev,
  670. "%s: get_conf_levels failed, err = %d\n",
  671. __func__, rc);
  672. return rc;
  673. }
  674. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  675. prtd->lsm_client->confidence_levels,
  676. LSM_MIN_CONFIDENCE_LEVELS);
  677. if (rc)
  678. dev_err(rtd->dev,
  679. "%s: Failed to set min_conf_levels, err = %d\n",
  680. __func__, rc);
  681. if (prtd->lsm_client->confidence_levels) {
  682. kfree(prtd->lsm_client->confidence_levels);
  683. prtd->lsm_client->confidence_levels = NULL;
  684. }
  685. }
  686. return rc;
  687. }
  688. static int msm_lsm_reg_model(struct snd_pcm_substream *substream,
  689. struct lsm_params_info_v2 *p_info)
  690. {
  691. struct snd_pcm_runtime *runtime = substream->runtime;
  692. struct lsm_priv *prtd = runtime->private_data;
  693. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  694. int rc = 0, stage_idx = p_info->stage_idx;
  695. struct lsm_sound_model *sm = NULL;
  696. size_t offset = sizeof(union param_hdrs);
  697. struct lsm_client *client = prtd->lsm_client;
  698. if (p_info->model_id != 0 &&
  699. p_info->param_type == LSM_REG_MULTI_SND_MODEL) {
  700. sm = kzalloc(sizeof(*sm), GFP_KERNEL);
  701. if (sm == NULL) {
  702. dev_err(rtd->dev, "%s: snd_model kzalloc failed\n", __func__);
  703. return -ENOMEM;
  704. }
  705. INIT_LIST_HEAD(&sm->list);
  706. rc = q6lsm_snd_model_buf_alloc(client, p_info->param_size, p_info, sm);
  707. if (rc) {
  708. dev_err(rtd->dev, "%s: snd_model buf alloc failed, size = %d\n",
  709. __func__, p_info->param_size);
  710. goto err_buf_alloc;
  711. }
  712. q6lsm_sm_set_param_data(client, p_info, &offset, sm);
  713. /*
  714. * For set_param, advance the sound model data with the
  715. * number of bytes required by param_data.
  716. */
  717. if (copy_from_user((u8 *)sm->data + offset,
  718. p_info->param_data, p_info->param_size)) {
  719. dev_err(rtd->dev,
  720. "%s: copy_from_user for snd_model %d failed, size = %d\n",
  721. __func__, p_info->model_id, p_info->param_size);
  722. rc = -EFAULT;
  723. goto err_copy;
  724. }
  725. /* Add this sound model to the list of multi sound models */
  726. list_add_tail(&sm->list, &client->stage_cfg[stage_idx].sound_models);
  727. rc = q6lsm_set_one_param(client, p_info, NULL,
  728. LSM_REG_MULTI_SND_MODEL);
  729. if (rc) {
  730. dev_err(rtd->dev,
  731. "%s: Failed to register snd_model %d, err = %d\n",
  732. __func__, p_info->model_id, rc);
  733. goto err_copy;
  734. }
  735. client->num_sound_models++;
  736. dev_dbg(rtd->dev,
  737. "%s: registered snd_model: %d, total num of snd_model: %d\n",
  738. __func__, p_info->model_id, client->num_sound_models);
  739. } else if (p_info->model_id == 0 &&
  740. p_info->param_type == LSM_REG_SND_MODEL) {
  741. sm = &client->stage_cfg[stage_idx].sound_model;
  742. rc = q6lsm_snd_model_buf_alloc(client, p_info->param_size, p_info, sm);
  743. if (rc) {
  744. dev_err(rtd->dev, "%s: snd_model buf alloc failed, size = %d\n",
  745. __func__, p_info->param_size);
  746. return rc;
  747. }
  748. q6lsm_sm_set_param_data(client, p_info, &offset, sm);
  749. /*
  750. * For set_param, advance the sound model data with the
  751. * number of bytes required by param_data.
  752. */
  753. if (copy_from_user((u8 *)sm->data + offset,
  754. p_info->param_data, p_info->param_size)) {
  755. dev_err(rtd->dev,
  756. "%s: copy_from_user for snd_model failed, size = %d\n",
  757. __func__, p_info->param_size);
  758. rc = -EFAULT;
  759. goto err_copy;
  760. }
  761. rc = q6lsm_set_one_param(client, p_info, NULL, LSM_REG_SND_MODEL);
  762. if (rc) {
  763. dev_err(rtd->dev, "%s: Failed to register snd_model, err = %d\n",
  764. __func__, rc);
  765. goto err_copy;
  766. }
  767. } else {
  768. dev_err(rtd->dev,
  769. "%s: snd_model id %d is invalid for param type %d\n",
  770. __func__, p_info->model_id, p_info->param_type);
  771. }
  772. return rc;
  773. err_copy:
  774. q6lsm_snd_model_buf_free(client, p_info, sm);
  775. err_buf_alloc:
  776. if (p_info->model_id != 0) {
  777. list_del(&sm->list);
  778. kfree(sm);
  779. sm = NULL;
  780. }
  781. return rc;
  782. }
  783. static int msm_lsm_dereg_model(struct snd_pcm_substream *substream,
  784. struct lsm_params_info_v2 *p_info)
  785. {
  786. struct snd_pcm_runtime *runtime = substream->runtime;
  787. struct lsm_priv *prtd = runtime->private_data;
  788. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  789. struct lsm_sound_model *sm = NULL;
  790. struct lsm_client *client = prtd->lsm_client;
  791. int rc = 0;
  792. if (p_info->model_id != 0 &&
  793. p_info->param_type == LSM_DEREG_MULTI_SND_MODEL) {
  794. list_for_each_entry(sm,
  795. &client->stage_cfg[p_info->stage_idx].sound_models,
  796. list) {
  797. dev_dbg(rtd->dev,
  798. "%s: current snd_model: %d, looking for snd_model %d\n",
  799. __func__, sm->model_id, p_info->model_id);
  800. if (sm->model_id == p_info->model_id)
  801. break;
  802. }
  803. if (sm->model_id == p_info->model_id) {
  804. rc = q6lsm_set_one_param(client, p_info, NULL,
  805. LSM_DEREG_MULTI_SND_MODEL);
  806. if (rc)
  807. dev_err(rtd->dev,
  808. "%s: Failed to deregister snd_model %d, err = %d\n",
  809. __func__, p_info->model_id, rc);
  810. q6lsm_snd_model_buf_free(client, p_info, sm);
  811. list_del(&sm->list);
  812. kfree(sm);
  813. sm = NULL;
  814. client->num_sound_models--;
  815. } else {
  816. rc = -EINVAL;
  817. dev_err(rtd->dev,
  818. "%s: Failed to find snd_model, invalid model_id %d\n",
  819. __func__, p_info->model_id);
  820. }
  821. } else if (p_info->model_id == 0 &&
  822. p_info->param_type == LSM_DEREG_SND_MODEL) {
  823. rc = q6lsm_set_one_param(client, p_info, NULL,
  824. LSM_DEREG_SND_MODEL);
  825. if (rc)
  826. dev_err(rtd->dev,
  827. "%s: Failed to deregister snd_model, err = %d\n",
  828. __func__, rc);
  829. sm = &client->stage_cfg[p_info->stage_idx].sound_model;
  830. q6lsm_snd_model_buf_free(client, p_info, sm);
  831. } else {
  832. rc = -EINVAL;
  833. dev_err(rtd->dev,
  834. "%s: snd_model id %d is invalid for param type %d\n",
  835. __func__, p_info->model_id, p_info->param_type);
  836. }
  837. return rc;
  838. }
  839. static int msm_lsm_set_custom(struct snd_pcm_substream *substream,
  840. struct lsm_params_info_v2 *p_info)
  841. {
  842. struct snd_pcm_runtime *runtime = substream->runtime;
  843. struct lsm_priv *prtd = runtime->private_data;
  844. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  845. u8 *data;
  846. int rc = 0;
  847. data = kzalloc(p_info->param_size, GFP_KERNEL);
  848. if (!data)
  849. return -ENOMEM;
  850. if (copy_from_user(data, p_info->param_data,
  851. p_info->param_size)) {
  852. dev_err(rtd->dev,
  853. "%s: copy_from_user failed for custom params, size = %d\n",
  854. __func__, p_info->param_size);
  855. rc = -EFAULT;
  856. goto err_ret;
  857. }
  858. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  859. data, LSM_CUSTOM_PARAMS);
  860. if (rc)
  861. dev_err(rtd->dev,
  862. "%s: Failed to set custom param, err = %d\n",
  863. __func__, rc);
  864. err_ret:
  865. kfree(data);
  866. return rc;
  867. }
  868. static int msm_lsm_check_and_set_lab_controls(struct snd_pcm_substream *substream,
  869. u32 enable, struct lsm_params_info_v2 *p_info)
  870. {
  871. struct snd_pcm_runtime *runtime = substream->runtime;
  872. struct lsm_priv *prtd = runtime->private_data;
  873. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  874. struct lsm_hw_params *out_hw_params = &prtd->lsm_client->out_hw_params;
  875. u8 *chmap = NULL;
  876. u32 ch_idx;
  877. int rc = 0, stage_idx = p_info->stage_idx;
  878. if (prtd->lsm_client->stage_cfg[stage_idx].lab_enable == enable) {
  879. dev_dbg(rtd->dev, "%s: Lab for session %d, stage %d already %s\n",
  880. __func__, prtd->lsm_client->session,
  881. stage_idx, enable ? "enabled" : "disabled");
  882. return rc;
  883. }
  884. chmap = kzalloc(out_hw_params->num_chs, GFP_KERNEL);
  885. if (!chmap)
  886. return -ENOMEM;
  887. rc = q6lsm_lab_control(prtd->lsm_client, enable, p_info);
  888. if (rc) {
  889. dev_err(rtd->dev, "%s: Failed to set lab_control param, err = %d\n",
  890. __func__, rc);
  891. goto fail;
  892. } else {
  893. if (LSM_IS_LAST_STAGE(prtd->lsm_client, stage_idx)) {
  894. rc = msm_lsm_lab_buffer_alloc(prtd,
  895. enable ? LAB_BUFFER_ALLOC : LAB_BUFFER_DEALLOC);
  896. if (rc) {
  897. dev_err(rtd->dev,
  898. "%s: msm_lsm_lab_buffer_alloc failed rc %d for %s\n",
  899. __func__, rc, enable ? "ALLOC" : "DEALLOC");
  900. goto fail;
  901. } else {
  902. /* set client level flag based on last stage control */
  903. prtd->lsm_client->lab_enable = enable;
  904. }
  905. }
  906. if (!rc)
  907. prtd->lsm_client->stage_cfg[stage_idx].lab_enable = enable;
  908. }
  909. /*
  910. * First channel to be read from lab is always the
  911. * best channel (0xff). For second channel onwards,
  912. * the channel indices are 0, 1, .. etc
  913. */
  914. chmap[0] = 0xFF;
  915. for (ch_idx = 1; ch_idx < out_hw_params->num_chs; ch_idx++)
  916. chmap[ch_idx] = ch_idx - 1;
  917. rc = q6lsm_lab_out_ch_cfg(prtd->lsm_client, chmap, p_info);
  918. if (rc)
  919. dev_err(rtd->dev, "%s: Failed to set lab out ch cfg %d\n",
  920. __func__, rc);
  921. fail:
  922. kfree(chmap);
  923. return rc;
  924. }
  925. static int msm_lsm_set_lab_control(struct snd_pcm_substream *substream,
  926. struct lsm_params_info_v2 *p_info)
  927. {
  928. struct snd_pcm_runtime *runtime = substream->runtime;
  929. struct lsm_priv *prtd = runtime->private_data;
  930. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  931. struct snd_lsm_lab_control lab_ctrl;
  932. int rc = 0;
  933. if (p_info->param_size != sizeof(lab_ctrl))
  934. return -EINVAL;
  935. if (prtd->lsm_client->started) {
  936. dev_err(rtd->dev, "%s: lab control sent after start\n", __func__);
  937. return -EAGAIN;
  938. }
  939. if (copy_from_user(&lab_ctrl, p_info->param_data,
  940. p_info->param_size)) {
  941. dev_err(rtd->dev,
  942. "%s: copy_from_user failed for lab_control params, size = %d\n",
  943. __func__, p_info->param_size);
  944. return -EFAULT;
  945. }
  946. rc = msm_lsm_check_and_set_lab_controls(substream, lab_ctrl.enable, p_info);
  947. return rc;
  948. }
  949. static int msm_lsm_set_poll_enable(struct snd_pcm_substream *substream,
  950. struct lsm_params_info_v2 *p_info)
  951. {
  952. struct snd_pcm_runtime *runtime = substream->runtime;
  953. struct lsm_priv *prtd = runtime->private_data;
  954. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  955. struct snd_lsm_poll_enable poll_enable;
  956. int rc = 0;
  957. if (p_info->param_size != sizeof(poll_enable)) {
  958. dev_err(rtd->dev,
  959. "%s: Invalid param_size %d\n",
  960. __func__, p_info->param_size);
  961. rc = -EINVAL;
  962. goto done;
  963. }
  964. if (copy_from_user(&poll_enable, p_info->param_data,
  965. sizeof(poll_enable))) {
  966. dev_err(rtd->dev,
  967. "%s: copy_from_user failed, size = %zd\n",
  968. __func__, sizeof(poll_enable));
  969. rc = -EFAULT;
  970. goto done;
  971. }
  972. if (prtd->lsm_client->poll_enable == poll_enable.poll_en) {
  973. dev_dbg(rtd->dev,
  974. "%s: Polling for session %d already %s\n",
  975. __func__, prtd->lsm_client->session,
  976. (poll_enable.poll_en ? "enabled" : "disabled"));
  977. rc = 0;
  978. goto done;
  979. }
  980. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  981. &poll_enable, LSM_POLLING_ENABLE);
  982. if (!rc) {
  983. prtd->lsm_client->poll_enable = poll_enable.poll_en;
  984. } else {
  985. dev_err(rtd->dev,
  986. "%s: Failed to set poll enable, err = %d\n",
  987. __func__, rc);
  988. }
  989. done:
  990. return rc;
  991. }
  992. static int msm_lsm_set_det_event_type(struct snd_pcm_substream *substream,
  993. struct lsm_params_info_v2 *p_info)
  994. {
  995. struct snd_lsm_det_event_type det_event_type;
  996. struct snd_pcm_runtime *runtime = substream->runtime;
  997. struct lsm_priv *prtd = runtime->private_data;
  998. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  999. int rc = 0;
  1000. if (p_info->param_size != sizeof(det_event_type)) {
  1001. dev_err(rtd->dev,
  1002. "%s: Invalid param_size %d\n",
  1003. __func__, p_info->param_size);
  1004. rc = -EINVAL;
  1005. goto done;
  1006. }
  1007. if (copy_from_user(&det_event_type, p_info->param_data,
  1008. sizeof(det_event_type))) {
  1009. dev_err(rtd->dev,
  1010. "%s: copy_from_user failed, size = %zd\n",
  1011. __func__, sizeof(det_event_type));
  1012. rc = -EFAULT;
  1013. goto done;
  1014. }
  1015. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  1016. &det_event_type, LSM_DET_EVENT_TYPE);
  1017. if (!rc)
  1018. prtd->lsm_client->event_type = det_event_type.event_type;
  1019. else
  1020. dev_err(rtd->dev,
  1021. "%s: Failed to set detection event type %s, err = %d\n",
  1022. __func__, (det_event_type.event_type ?
  1023. "LSM_DET_EVENT_TYPE_GENERIC" :
  1024. "LSM_DET_EVENT_TYPE_LEGACY"), rc);
  1025. done:
  1026. return rc;
  1027. }
  1028. static int msm_lsm_process_params(struct snd_pcm_substream *substream,
  1029. struct lsm_params_info_v2 *p_info)
  1030. {
  1031. struct snd_pcm_runtime *runtime = substream->runtime;
  1032. struct lsm_priv *prtd = runtime->private_data;
  1033. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1034. int rc = 0;
  1035. dev_dbg(rtd->dev,
  1036. "%s: mid=0x%x, pid=0x%x, iid=0x%x, stage_idx=%d, size=0x%x, type=%d\n",
  1037. __func__, p_info->module_id, p_info->param_id, p_info->instance_id,
  1038. p_info->stage_idx, p_info->param_size, p_info->param_type);
  1039. if (!prtd->lsm_client ||
  1040. prtd->lsm_client->num_stages <= p_info->stage_idx) {
  1041. dev_err(rtd->dev,
  1042. "%s: invalid stage_idx(%d) for client(%p) having num_stages(%d)\n",
  1043. __func__, p_info->stage_idx, prtd->lsm_client,
  1044. prtd->lsm_client ? prtd->lsm_client->num_stages : 0);
  1045. return -EINVAL;
  1046. }
  1047. if (p_info->param_type == LSM_REG_MULTI_SND_MODEL &&
  1048. prtd->lsm_client->num_sound_models == LSM_MAX_SOUND_MODELS_SUPPORTED) {
  1049. dev_err(rtd->dev,
  1050. "%s: maximum supported sound models(8) have already reached\n",
  1051. __func__);
  1052. return -EINVAL;
  1053. }
  1054. if (p_info->param_type == LSM_DEREG_MULTI_SND_MODEL &&
  1055. prtd->lsm_client->num_sound_models == 0) {
  1056. dev_err(rtd->dev,
  1057. "%s: no available sound model to be deregistered\n",
  1058. __func__);
  1059. return -EINVAL;
  1060. }
  1061. switch (p_info->param_type) {
  1062. case LSM_ENDPOINT_DETECT_THRESHOLD:
  1063. rc = msm_lsm_set_epd(substream, p_info);
  1064. break;
  1065. case LSM_OPERATION_MODE:
  1066. rc = msm_lsm_set_mode(substream, p_info);
  1067. break;
  1068. case LSM_GAIN:
  1069. rc = msm_lsm_set_gain(substream, p_info);
  1070. break;
  1071. case LSM_MIN_CONFIDENCE_LEVELS:
  1072. case LSM_MULTI_SND_MODEL_CONFIDENCE_LEVELS:
  1073. rc = msm_lsm_set_conf(substream, p_info);
  1074. break;
  1075. case LSM_REG_SND_MODEL:
  1076. case LSM_REG_MULTI_SND_MODEL:
  1077. rc = msm_lsm_reg_model(substream, p_info);
  1078. break;
  1079. case LSM_DEREG_SND_MODEL:
  1080. case LSM_DEREG_MULTI_SND_MODEL:
  1081. rc = msm_lsm_dereg_model(substream, p_info);
  1082. break;
  1083. case LSM_CUSTOM_PARAMS:
  1084. rc = msm_lsm_set_custom(substream, p_info);
  1085. break;
  1086. case LSM_POLLING_ENABLE:
  1087. rc = msm_lsm_set_poll_enable(substream, p_info);
  1088. break;
  1089. case LSM_DET_EVENT_TYPE:
  1090. rc = msm_lsm_set_det_event_type(substream, p_info);
  1091. break;
  1092. case LSM_LAB_CONTROL:
  1093. rc = msm_lsm_set_lab_control(substream, p_info);
  1094. break;
  1095. default:
  1096. dev_err(rtd->dev,
  1097. "%s: Invalid param_type %d\n",
  1098. __func__, p_info->param_type);
  1099. rc = -EINVAL;
  1100. break;
  1101. }
  1102. if (rc) {
  1103. pr_err("%s: set_param fail for param_type %d\n",
  1104. __func__, p_info->param_type);
  1105. }
  1106. return rc;
  1107. }
  1108. static int msm_lsm_start_lab_buffer(struct lsm_priv *prtd, uint16_t status)
  1109. {
  1110. struct lsm_client *lsm_client = prtd->lsm_client;
  1111. int rc = 0;
  1112. if (lsm_client && lsm_client->lab_enable &&
  1113. !lsm_client->lab_started &&
  1114. status == LSM_VOICE_WAKEUP_STATUS_DETECTED) {
  1115. atomic_set(&prtd->read_abort, 0);
  1116. atomic_set(&prtd->buf_count, 0);
  1117. prtd->appl_cnt = 0;
  1118. prtd->dma_write = 0;
  1119. prtd->xrun_count = 0;
  1120. prtd->xrun_index = 0;
  1121. rc = msm_lsm_queue_lab_buffer(prtd, 0);
  1122. if (rc)
  1123. pr_err("%s: Queue buffer failed for lab rc = %d\n",
  1124. __func__, rc);
  1125. else
  1126. prtd->lsm_client->lab_started = true;
  1127. }
  1128. return rc;
  1129. }
  1130. static int msm_lsm_ioctl_shared(struct snd_pcm_substream *substream,
  1131. unsigned int cmd, void *arg)
  1132. {
  1133. struct snd_soc_pcm_runtime *rtd;
  1134. unsigned long flags = 0;
  1135. int ret;
  1136. struct snd_lsm_sound_model_v2 snd_model_v2;
  1137. struct snd_lsm_session_data session_data;
  1138. struct snd_lsm_session_data_v2 ses_data_v2 = {0};
  1139. int rc = 0, stage_idx;
  1140. int xchg = 0;
  1141. struct snd_pcm_runtime *runtime;
  1142. struct lsm_priv *prtd;
  1143. struct snd_lsm_detection_params det_params;
  1144. uint32_t max_detection_stages_supported = LSM_MAX_STAGES_PER_SESSION;
  1145. if (!substream || !substream->private_data) {
  1146. pr_err("%s: Invalid %s\n", __func__,
  1147. (!substream) ? "substream" : "private_data");
  1148. return -EINVAL;
  1149. }
  1150. runtime = substream->runtime;
  1151. prtd = runtime->private_data;
  1152. rtd = substream->private_data;
  1153. dev_dbg(rtd->dev, "%s: enter, cmd %x\n", __func__, cmd);
  1154. switch (cmd) {
  1155. case SNDRV_LSM_SET_SESSION_DATA:
  1156. case SNDRV_LSM_SET_SESSION_DATA_V2:
  1157. if (cmd == SNDRV_LSM_SET_SESSION_DATA) {
  1158. dev_dbg(rtd->dev, "%s: set session data\n", __func__);
  1159. rc = copy_from_user(&session_data, arg, sizeof(session_data));
  1160. if (!rc) {
  1161. ses_data_v2.app_id = session_data.app_id;
  1162. ses_data_v2.num_stages = 1;
  1163. }
  1164. } else {
  1165. dev_dbg(rtd->dev, "%s: set session data_v2\n", __func__);
  1166. rc = copy_from_user(&ses_data_v2, arg, sizeof(ses_data_v2));
  1167. }
  1168. if (rc) {
  1169. dev_err(rtd->dev, "%s: %s: copy_from_user failed\n",
  1170. __func__, "LSM_SET_SESSION_DATA(_V2)");
  1171. return -EFAULT;
  1172. }
  1173. if (ses_data_v2.app_id != LSM_VOICE_WAKEUP_APP_ID_V2) {
  1174. dev_err(rtd->dev,
  1175. "%s:Invalid App id %d for Listen client\n",
  1176. __func__, ses_data_v2.app_id);
  1177. rc = -EINVAL;
  1178. break;
  1179. }
  1180. /*
  1181. * Before validating num_stages from user argument.
  1182. * Check ADSP support for multi-stage session,
  1183. * and reset max_detection_stages_supported to "1" if required.
  1184. */
  1185. if (!q6lsm_adsp_supports_multi_stage_detection()) {
  1186. dev_dbg(rtd->dev,
  1187. "%s: multi-stage session not supported by adsp\n", __func__);
  1188. max_detection_stages_supported = 1;
  1189. }
  1190. if (ses_data_v2.num_stages <= 0 ||
  1191. ses_data_v2.num_stages > max_detection_stages_supported) {
  1192. dev_err(rtd->dev,
  1193. "%s: Unsupported number of stages req(%d)/max(%d)\n",
  1194. __func__, ses_data_v2.num_stages,
  1195. max_detection_stages_supported);
  1196. rc = -EINVAL;
  1197. break;
  1198. }
  1199. prtd->lsm_client->app_id = ses_data_v2.app_id;
  1200. prtd->lsm_client->num_stages = ses_data_v2.num_stages;
  1201. for (stage_idx = LSM_STAGE_INDEX_FIRST;
  1202. stage_idx < ses_data_v2.num_stages; stage_idx++) {
  1203. prtd->lsm_client->stage_cfg[stage_idx].app_type =
  1204. ses_data_v2.stage_info[stage_idx].app_type;
  1205. prtd->lsm_client->stage_cfg[stage_idx].lpi_enable =
  1206. ses_data_v2.stage_info[stage_idx].lpi_enable;
  1207. }
  1208. ret = q6lsm_open(prtd->lsm_client, ses_data_v2.app_id);
  1209. if (ret < 0) {
  1210. dev_err(rtd->dev,
  1211. "%s: lsm open failed, %d\n",
  1212. __func__, ret);
  1213. __pm_relax(prtd->ws);
  1214. return ret;
  1215. }
  1216. prtd->lsm_client->opened = true;
  1217. dev_dbg(rtd->dev, "%s: Session_ID = %d, APP ID = %d, Num stages %d\n",
  1218. __func__,
  1219. prtd->lsm_client->session,
  1220. prtd->lsm_client->app_id,
  1221. prtd->lsm_client->num_stages);
  1222. break;
  1223. case SNDRV_LSM_REG_SND_MODEL_V2: {
  1224. /*
  1225. * With multi-stage support sm buff allocation/free usage param info
  1226. * to check stage index for which this sound model is being set, and
  1227. * to check whether sm data is sent using set param command or not.
  1228. * Hence, set param ids to '0' to indicate allocation is for legacy
  1229. * reg_sm cmd, where buffer for param header need not be allocated,
  1230. * also set stage index to LSM_STAGE_INDEX_FIRST.
  1231. */
  1232. struct lsm_params_info_v2 p_info = {0};
  1233. struct lsm_sound_model *sm = NULL;
  1234. p_info.stage_idx = LSM_STAGE_INDEX_FIRST;
  1235. p_info.param_type = LSM_DEREG_SND_MODEL;
  1236. sm = &prtd->lsm_client->stage_cfg[p_info.stage_idx].sound_model;
  1237. dev_dbg(rtd->dev, "%s: Registering sound model V2\n",
  1238. __func__);
  1239. memcpy(&snd_model_v2, arg,
  1240. sizeof(struct snd_lsm_sound_model_v2));
  1241. if (snd_model_v2.num_confidence_levels >
  1242. MAX_NUM_CONFIDENCE) {
  1243. dev_err(rtd->dev,
  1244. "%s: Invalid conf_levels = %d, maximum allowed = %d\n",
  1245. __func__, snd_model_v2.num_confidence_levels,
  1246. MAX_NUM_CONFIDENCE);
  1247. rc = -EINVAL;
  1248. break;
  1249. }
  1250. rc = q6lsm_snd_model_buf_alloc(prtd->lsm_client,
  1251. snd_model_v2.data_size, &p_info, sm);
  1252. if (rc) {
  1253. dev_err(rtd->dev,
  1254. "%s: q6lsm buffer alloc failed V2, size %d\n",
  1255. __func__, snd_model_v2.data_size);
  1256. break;
  1257. }
  1258. if (copy_from_user(sm->data, snd_model_v2.data,
  1259. snd_model_v2.data_size)) {
  1260. dev_err(rtd->dev,
  1261. "%s: copy from user data failed\n"
  1262. "data %pK size %d\n", __func__,
  1263. snd_model_v2.data, snd_model_v2.data_size);
  1264. q6lsm_snd_model_buf_free(prtd->lsm_client, &p_info, sm);
  1265. rc = -EFAULT;
  1266. break;
  1267. }
  1268. dev_dbg(rtd->dev, "SND Model Magic no byte[0] %x,\n"
  1269. "byte[1] %x, byte[2] %x byte[3] %x\n",
  1270. snd_model_v2.data[0], snd_model_v2.data[1],
  1271. snd_model_v2.data[2], snd_model_v2.data[3]);
  1272. prtd->lsm_client->num_confidence_levels =
  1273. snd_model_v2.num_confidence_levels;
  1274. rc = msm_lsm_get_conf_levels(prtd->lsm_client,
  1275. snd_model_v2.confidence_level);
  1276. if (rc) {
  1277. dev_err(rtd->dev,
  1278. "%s: get_conf_levels failed, err = %d\n",
  1279. __func__, rc);
  1280. break;
  1281. }
  1282. rc = q6lsm_register_sound_model(prtd->lsm_client,
  1283. snd_model_v2.detection_mode,
  1284. snd_model_v2.detect_failure);
  1285. if (rc < 0) {
  1286. dev_err(rtd->dev,
  1287. "%s: Register snd Model v2 failed =%d\n",
  1288. __func__, rc);
  1289. q6lsm_snd_model_buf_free(prtd->lsm_client, &p_info, sm);
  1290. }
  1291. if (prtd->lsm_client->confidence_levels) {
  1292. kfree(prtd->lsm_client->confidence_levels);
  1293. prtd->lsm_client->confidence_levels = NULL;
  1294. }
  1295. break;
  1296. }
  1297. case SNDRV_LSM_SET_PARAMS:
  1298. dev_dbg(rtd->dev, "%s: set_params\n", __func__);
  1299. memcpy(&det_params, arg,
  1300. sizeof(det_params));
  1301. if (det_params.num_confidence_levels >
  1302. MAX_NUM_CONFIDENCE) {
  1303. rc = -EINVAL;
  1304. break;
  1305. }
  1306. prtd->lsm_client->num_confidence_levels =
  1307. det_params.num_confidence_levels;
  1308. rc = msm_lsm_get_conf_levels(prtd->lsm_client,
  1309. det_params.conf_level);
  1310. if (rc) {
  1311. dev_err(rtd->dev,
  1312. "%s: Failed to get conf_levels, err = %d\n",
  1313. __func__, rc);
  1314. break;
  1315. }
  1316. rc = q6lsm_set_data(prtd->lsm_client,
  1317. det_params.detect_mode,
  1318. det_params.detect_failure);
  1319. if (rc)
  1320. dev_err(rtd->dev,
  1321. "%s: Failed to set params, err = %d\n",
  1322. __func__, rc);
  1323. if (prtd->lsm_client->confidence_levels) {
  1324. kfree(prtd->lsm_client->confidence_levels);
  1325. prtd->lsm_client->confidence_levels = NULL;
  1326. }
  1327. break;
  1328. case SNDRV_LSM_DEREG_SND_MODEL:
  1329. dev_dbg(rtd->dev, "%s: Deregistering sound model\n",
  1330. __func__);
  1331. rc = q6lsm_deregister_sound_model(prtd->lsm_client);
  1332. if (rc)
  1333. dev_err(rtd->dev,
  1334. "%s: Sound model de-register failed, err = %d\n",
  1335. __func__, rc);
  1336. break;
  1337. case SNDRV_LSM_EVENT_STATUS:
  1338. case SNDRV_LSM_EVENT_STATUS_V3: {
  1339. uint32_t ts_lsw, ts_msw;
  1340. uint16_t status = 0, payload_size = 0;
  1341. dev_dbg(rtd->dev, "%s: Get event status cmd %xx\n", __func__, cmd);
  1342. atomic_set(&prtd->event_wait_stop, 0);
  1343. /*
  1344. * Release the api lock before wait to allow
  1345. * other IOCTLs to be invoked while waiting
  1346. * for event
  1347. */
  1348. mutex_unlock(&prtd->lsm_api_lock);
  1349. rc = wait_event_freezable(prtd->event_wait,
  1350. (cmpxchg(&prtd->event_avail, 1, 0) ||
  1351. (xchg = atomic_cmpxchg(&prtd->event_wait_stop,
  1352. 1, 0))));
  1353. dev_dbg(rtd->dev, "%s: wait event is done\n", __func__);
  1354. mutex_lock(&prtd->lsm_api_lock);
  1355. dev_dbg(rtd->dev, "%s: wait_event_freezable %d event_wait_stop %d\n",
  1356. __func__, rc, xchg);
  1357. if (!rc && !xchg) {
  1358. dev_dbg(rtd->dev, "%s: New event available %ld\n",
  1359. __func__, prtd->event_avail);
  1360. spin_lock_irqsave(&prtd->event_lock, flags);
  1361. if (prtd->event_status) {
  1362. payload_size = prtd->event_status->payload_size;
  1363. ts_lsw = prtd->event_status->timestamp_lsw;
  1364. ts_msw = prtd->event_status->timestamp_msw;
  1365. status = prtd->event_status->status;
  1366. spin_unlock_irqrestore(&prtd->event_lock,
  1367. flags);
  1368. } else {
  1369. spin_unlock_irqrestore(&prtd->event_lock,
  1370. flags);
  1371. rc = -EINVAL;
  1372. dev_err(rtd->dev,
  1373. "%s: prtd->event_status is NULL\n",
  1374. __func__);
  1375. break;
  1376. }
  1377. if (cmd == SNDRV_LSM_EVENT_STATUS) {
  1378. struct snd_lsm_event_status *user = arg;
  1379. if (user->payload_size < payload_size) {
  1380. dev_dbg(rtd->dev,
  1381. "%s: provided %d bytes isn't enough, needs %d bytes\n",
  1382. __func__, user->payload_size,
  1383. payload_size);
  1384. rc = -ENOMEM;
  1385. } else {
  1386. user->status = status;
  1387. user->payload_size = payload_size;
  1388. memcpy(user->payload,
  1389. prtd->event_status->payload,
  1390. payload_size);
  1391. }
  1392. } else {
  1393. struct snd_lsm_event_status_v3 *user_v3 = arg;
  1394. if (user_v3->payload_size < payload_size) {
  1395. dev_dbg(rtd->dev,
  1396. "%s: provided %d bytes isn't enough, needs %d bytes\n",
  1397. __func__, user_v3->payload_size,
  1398. payload_size);
  1399. rc = -ENOMEM;
  1400. } else {
  1401. user_v3->timestamp_lsw = ts_lsw;
  1402. user_v3->timestamp_msw = ts_msw;
  1403. user_v3->status = status;
  1404. user_v3->payload_size = payload_size;
  1405. memcpy(user_v3->payload,
  1406. prtd->event_status->payload,
  1407. payload_size);
  1408. }
  1409. }
  1410. if (!rc)
  1411. rc = msm_lsm_start_lab_buffer(prtd, status);
  1412. } else if (xchg) {
  1413. dev_dbg(rtd->dev, "%s: Wait aborted\n", __func__);
  1414. rc = 0;
  1415. }
  1416. break;
  1417. }
  1418. case SNDRV_LSM_GENERIC_DET_EVENT: {
  1419. struct snd_lsm_event_status *user = arg;
  1420. uint16_t status = 0;
  1421. uint16_t payload_size = 0;
  1422. dev_dbg(rtd->dev,
  1423. "%s: SNDRV_LSM_GENERIC_DET_EVENT\n", __func__);
  1424. atomic_set(&prtd->event_wait_stop, 0);
  1425. /*
  1426. * Release the api lock before wait to allow
  1427. * other IOCTLs to be invoked while waiting
  1428. * for event
  1429. */
  1430. mutex_unlock(&prtd->lsm_api_lock);
  1431. rc = wait_event_freezable(prtd->event_wait,
  1432. (cmpxchg(&prtd->event_avail, 1, 0) ||
  1433. (xchg = atomic_cmpxchg(&prtd->event_wait_stop,
  1434. 1, 0))));
  1435. mutex_lock(&prtd->lsm_api_lock);
  1436. dev_dbg(rtd->dev, "%s: wait_event_freezable %d event_wait_stop %d\n",
  1437. __func__, rc, xchg);
  1438. if (!rc && !xchg) {
  1439. dev_dbg(rtd->dev, "%s: %s: New event available %ld\n",
  1440. __func__, "SNDRV_LSM_GENERIC_DET_EVENT",
  1441. prtd->event_avail);
  1442. spin_lock_irqsave(&prtd->event_lock, flags);
  1443. if (prtd->det_event) {
  1444. payload_size = prtd->det_event->payload_size;
  1445. status = prtd->det_event->status;
  1446. spin_unlock_irqrestore(&prtd->event_lock,
  1447. flags);
  1448. } else {
  1449. spin_unlock_irqrestore(&prtd->event_lock,
  1450. flags);
  1451. dev_err(rtd->dev,
  1452. "%s: %s: prtd->event_status is NULL\n",
  1453. __func__,
  1454. "SNDRV_LSM_GENERIC_DET_EVENT");
  1455. rc = -EINVAL;
  1456. break;
  1457. }
  1458. if (user->payload_size < payload_size) {
  1459. dev_err(rtd->dev,
  1460. "%s: provided %d bytes isn't enough, needs %d bytes\n",
  1461. __func__, user->payload_size,
  1462. payload_size);
  1463. rc = -ENOMEM;
  1464. break;
  1465. }
  1466. user->status = status;
  1467. user->payload_size = payload_size;
  1468. memcpy(user->payload, prtd->det_event->payload,
  1469. payload_size);
  1470. rc = msm_lsm_start_lab_buffer(prtd, status);
  1471. } else if (xchg) {
  1472. dev_dbg(rtd->dev, "%s: %s: Wait aborted\n",
  1473. __func__, "SNDRV_LSM_GENERIC_DET_EVENT");
  1474. rc = 0;
  1475. }
  1476. break;
  1477. }
  1478. case SNDRV_LSM_ABORT_EVENT:
  1479. dev_dbg(rtd->dev, "%s: Aborting event status wait\n",
  1480. __func__);
  1481. atomic_set(&prtd->event_wait_stop, 1);
  1482. wake_up(&prtd->event_wait);
  1483. break;
  1484. case SNDRV_LSM_START:
  1485. dev_dbg(rtd->dev, "%s: Starting LSM client session\n",
  1486. __func__);
  1487. if (!prtd->lsm_client->started) {
  1488. rc = q6lsm_start(prtd->lsm_client, true);
  1489. if (!rc) {
  1490. prtd->lsm_client->started = true;
  1491. dev_dbg(rtd->dev, "%s: LSM client session started\n",
  1492. __func__);
  1493. }
  1494. }
  1495. break;
  1496. case SNDRV_LSM_STOP: {
  1497. dev_dbg(rtd->dev,
  1498. "%s: Stopping LSM client session\n",
  1499. __func__);
  1500. if (prtd->lsm_client->started) {
  1501. if (prtd->lsm_client->lab_enable) {
  1502. atomic_set(&prtd->read_abort, 1);
  1503. if (prtd->lsm_client->lab_started) {
  1504. rc = q6lsm_stop_lab(prtd->lsm_client);
  1505. if (rc)
  1506. dev_err(rtd->dev,
  1507. "%s: stop lab failed rc %d\n",
  1508. __func__, rc);
  1509. prtd->lsm_client->lab_started = false;
  1510. }
  1511. }
  1512. if (!atomic_read(&prtd->read_abort)) {
  1513. dev_dbg(rtd->dev,
  1514. "%s: set read_abort to stop buffering\n", __func__);
  1515. atomic_set(&prtd->read_abort, 1);
  1516. }
  1517. rc = q6lsm_stop(prtd->lsm_client, true);
  1518. if (!rc)
  1519. dev_dbg(rtd->dev,
  1520. "%s: LSM client session stopped %d\n",
  1521. __func__, rc);
  1522. prtd->lsm_client->started = false;
  1523. }
  1524. break;
  1525. }
  1526. case SNDRV_LSM_LAB_CONTROL: {
  1527. u32 enable = 0;
  1528. struct lsm_params_info_v2 p_info = {0};
  1529. if (prtd->lsm_client->num_stages > 1) {
  1530. dev_err(rtd->dev, "%s: %s: not supported for multi stage session\n",
  1531. __func__, "LSM_LAB_CONTROL");
  1532. __pm_relax(prtd->ws);
  1533. return -EINVAL;
  1534. }
  1535. if (copy_from_user(&enable, arg, sizeof(enable))) {
  1536. dev_err(rtd->dev, "%s: %s: copy_frm_user failed\n",
  1537. __func__, "LSM_LAB_CONTROL");
  1538. __pm_relax(prtd->ws);
  1539. return -EFAULT;
  1540. }
  1541. dev_dbg(rtd->dev, "%s: ioctl %s, enable = %d\n",
  1542. __func__, "SNDRV_LSM_LAB_CONTROL", enable);
  1543. if (prtd->lsm_client->started) {
  1544. dev_err(rtd->dev, "%s: ioctl %s issued after start",
  1545. __func__, "SNDRV_LSM_LAB_CONTROL");
  1546. rc = -EINVAL;
  1547. break;
  1548. }
  1549. /*
  1550. * With multi-stage support lab control needs to set param info
  1551. * specifying stage index for which this lab control is issued,
  1552. * along with values of module/instance ids applicable for the stage.
  1553. * Hence, set param info with default lab module/instance ids, and
  1554. * set stage index to LSM_STAGE_INDEX_FIRST.
  1555. */
  1556. p_info.param_type = LSM_LAB_CONTROL;
  1557. p_info.module_id = LSM_MODULE_ID_LAB;
  1558. p_info.instance_id = INSTANCE_ID_0;
  1559. p_info.stage_idx = LSM_STAGE_INDEX_FIRST;
  1560. p_info.param_size = 0;
  1561. rc = msm_lsm_check_and_set_lab_controls(substream, enable, &p_info);
  1562. break;
  1563. }
  1564. case SNDRV_LSM_STOP_LAB:
  1565. dev_dbg(rtd->dev, "%s: stopping LAB\n", __func__);
  1566. if (prtd->lsm_client->lab_enable &&
  1567. prtd->lsm_client->lab_started) {
  1568. atomic_set(&prtd->read_abort, 1);
  1569. rc = q6lsm_stop_lab(prtd->lsm_client);
  1570. if (rc)
  1571. dev_err(rtd->dev,
  1572. "%s: Lab stop failed for session %d rc %d\n",
  1573. __func__,
  1574. prtd->lsm_client->session, rc);
  1575. prtd->lsm_client->lab_started = false;
  1576. }
  1577. break;
  1578. case SNDRV_LSM_SET_PORT:
  1579. dev_dbg(rtd->dev, "%s: set LSM port\n", __func__);
  1580. rc = q6lsm_set_port_connected(prtd->lsm_client);
  1581. break;
  1582. case SNDRV_LSM_SET_FWK_MODE_CONFIG: {
  1583. u32 mode;
  1584. if (copy_from_user(&mode, arg, sizeof(mode))) {
  1585. dev_err(rtd->dev, "%s: %s: copy_frm_user failed\n",
  1586. __func__, "LSM_SET_FWK_MODE_CONFIG");
  1587. __pm_relax(prtd->ws);
  1588. return -EFAULT;
  1589. }
  1590. dev_dbg(rtd->dev, "%s: ioctl %s, enable = %d\n",
  1591. __func__, "SNDRV_LSM_SET_FWK_MODE_CONFIG", mode);
  1592. if (prtd->lsm_client->event_mode == mode) {
  1593. dev_dbg(rtd->dev,
  1594. "%s: mode for %d already set to %d\n",
  1595. __func__, prtd->lsm_client->session, mode);
  1596. rc = 0;
  1597. } else {
  1598. dev_dbg(rtd->dev, "%s: Event mode = %d\n",
  1599. __func__, mode);
  1600. rc = q6lsm_set_fwk_mode_cfg(prtd->lsm_client, mode);
  1601. if (!rc)
  1602. prtd->lsm_client->event_mode = mode;
  1603. else
  1604. dev_err(rtd->dev,
  1605. "%s: set event mode failed %d\n",
  1606. __func__, rc);
  1607. }
  1608. break;
  1609. }
  1610. case SNDRV_LSM_SET_INPUT_HW_PARAMS: {
  1611. struct lsm_hw_params *in_params;
  1612. struct snd_lsm_input_hw_params params;
  1613. if (copy_from_user(&params, arg, sizeof(params))) {
  1614. dev_err(rtd->dev, "%s: %s: copy_from_user failed\n",
  1615. __func__, "LSM_SET_INPUT_HW_PARAMS");
  1616. __pm_relax(prtd->ws);
  1617. return -EFAULT;
  1618. }
  1619. in_params = &prtd->lsm_client->in_hw_params;
  1620. in_params->sample_rate = params.sample_rate;
  1621. in_params->sample_size = params.bit_width;
  1622. in_params->num_chs = params.num_channels;
  1623. break;
  1624. }
  1625. default:
  1626. dev_dbg(rtd->dev,
  1627. "%s: Falling into default snd_lib_ioctl cmd 0x%x\n",
  1628. __func__, cmd);
  1629. rc = snd_pcm_lib_ioctl(substream, cmd, arg);
  1630. break;
  1631. }
  1632. if (!rc)
  1633. dev_dbg(rtd->dev, "%s: leave (%d)\n",
  1634. __func__, rc);
  1635. else
  1636. dev_err(rtd->dev, "%s: cmd 0x%x failed %d\n",
  1637. __func__, cmd, rc);
  1638. __pm_relax(prtd->ws);
  1639. return rc;
  1640. }
  1641. static int msm_lsm_check_event_type(struct lsm_client *lsm_client,
  1642. unsigned int cmd)
  1643. {
  1644. int err = 0;
  1645. uint32_t event_type = lsm_client->event_type;
  1646. if (cmd == SNDRV_LSM_EVENT_STATUS &&
  1647. event_type != LSM_DET_EVENT_TYPE_LEGACY) {
  1648. pr_err("%s: %s: Invalid event request\n",
  1649. __func__, "SNDRV_LSM_EVENT_STATUS");
  1650. err = -EINVAL;
  1651. } else if (cmd == SNDRV_LSM_GENERIC_DET_EVENT &&
  1652. event_type != LSM_DET_EVENT_TYPE_GENERIC) {
  1653. pr_err("%s: %s: Invalid event request\n",
  1654. __func__, "SNDRV_LSM_GENERIC_DET_EVENT");
  1655. err = -EINVAL;
  1656. }
  1657. return err;
  1658. }
  1659. #ifdef CONFIG_COMPAT
  1660. struct snd_lsm_event_status32 {
  1661. u16 status;
  1662. u16 payload_size;
  1663. u8 payload[0];
  1664. };
  1665. struct snd_lsm_event_status_v3_32 {
  1666. u32 timestamp_lsw;
  1667. u32 timestamp_msw;
  1668. u16 status;
  1669. u16 payload_size;
  1670. u8 payload[0];
  1671. };
  1672. struct snd_lsm_sound_model_v2_32 {
  1673. compat_uptr_t data;
  1674. compat_uptr_t confidence_level;
  1675. u32 data_size;
  1676. enum lsm_detection_mode detection_mode;
  1677. u8 num_confidence_levels;
  1678. bool detect_failure;
  1679. };
  1680. struct snd_lsm_detection_params_32 {
  1681. compat_uptr_t conf_level;
  1682. enum lsm_detection_mode detect_mode;
  1683. u8 num_confidence_levels;
  1684. bool detect_failure;
  1685. };
  1686. struct lsm_params_info_32 {
  1687. u32 module_id;
  1688. u32 param_id;
  1689. u32 param_size;
  1690. compat_uptr_t param_data;
  1691. uint32_t param_type;
  1692. };
  1693. struct lsm_params_info_v2_32 {
  1694. u32 module_id;
  1695. u32 param_id;
  1696. u32 param_size;
  1697. compat_uptr_t param_data;
  1698. uint32_t param_type;
  1699. u16 instance_id;
  1700. u16 stage_idx;
  1701. u32 model_id;
  1702. };
  1703. struct snd_lsm_module_params_32 {
  1704. compat_uptr_t params;
  1705. u32 num_params;
  1706. u32 data_size;
  1707. };
  1708. enum {
  1709. SNDRV_LSM_REG_SND_MODEL_V2_32 =
  1710. _IOW('U', 0x07, struct snd_lsm_sound_model_v2_32),
  1711. SNDRV_LSM_SET_PARAMS_32 =
  1712. _IOW('U', 0x0A, struct snd_lsm_detection_params_32),
  1713. SNDRV_LSM_SET_MODULE_PARAMS_32 =
  1714. _IOW('U', 0x0B, struct snd_lsm_module_params_32),
  1715. SNDRV_LSM_EVENT_STATUS_V3_32 =
  1716. _IOW('U', 0x0F, struct snd_lsm_event_status_v3_32),
  1717. SNDRV_LSM_SET_MODULE_PARAMS_V2_32 =
  1718. _IOW('U', 0x13, struct snd_lsm_module_params_32),
  1719. };
  1720. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1721. static int msm_lsm_ioctl_compat(struct snd_pcm_substream *substream,
  1722. unsigned int cmd, void __user *arg)
  1723. {
  1724. struct snd_pcm_runtime *runtime;
  1725. struct lsm_priv *prtd;
  1726. struct snd_soc_pcm_runtime *rtd;
  1727. int err = 0;
  1728. u32 size = 0;
  1729. if (PCM_RUNTIME_CHECK(substream))
  1730. return -ENXIO;
  1731. if (!substream || !substream->private_data) {
  1732. pr_err("%s: Invalid %s\n", __func__,
  1733. (!substream) ? "substream" : "private_data");
  1734. return -EINVAL;
  1735. }
  1736. runtime = substream->runtime;
  1737. rtd = substream->private_data;
  1738. prtd = runtime->private_data;
  1739. mutex_lock(&prtd->lsm_api_lock);
  1740. switch (cmd) {
  1741. case SNDRV_LSM_EVENT_STATUS:
  1742. case SNDRV_LSM_GENERIC_DET_EVENT: {
  1743. struct snd_lsm_event_status userarg32, *user32 = NULL;
  1744. struct snd_lsm_event_status *user = NULL;
  1745. dev_dbg(rtd->dev,
  1746. "%s: %s\n", __func__,
  1747. (cmd == SNDRV_LSM_EVENT_STATUS) ?
  1748. "SNDRV_LSM_EVENT_STATUS" :
  1749. "SNDRV_LSM_GENERIC_DET_EVENT");
  1750. err = msm_lsm_check_event_type(prtd->lsm_client, cmd);
  1751. if (err)
  1752. goto done;
  1753. if (copy_from_user(&userarg32, arg, sizeof(userarg32))) {
  1754. dev_err(rtd->dev, "%s: %s: Failed to copy from user\n",
  1755. __func__, (cmd == SNDRV_LSM_EVENT_STATUS) ?
  1756. "SNDRV_LSM_EVENT_STATUS" :
  1757. "SNDRV_LSM_GENERIC_DET_EVENT");
  1758. err = -EFAULT;
  1759. goto done;
  1760. }
  1761. if (userarg32.payload_size >
  1762. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  1763. dev_err(rtd->dev,
  1764. "%s: payload_size %d is invalid, max allowed = %d\n",
  1765. __func__, userarg32.payload_size,
  1766. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  1767. err = -EINVAL;
  1768. goto done;
  1769. }
  1770. size = sizeof(*user) + userarg32.payload_size;
  1771. user = kzalloc(size, GFP_KERNEL);
  1772. if (!user) {
  1773. err = -ENOMEM;
  1774. goto done;
  1775. }
  1776. user->payload_size = userarg32.payload_size;
  1777. err = msm_lsm_ioctl_shared(substream, cmd, user);
  1778. if (err) {
  1779. dev_err(rtd->dev,
  1780. "%s: msm_lsm_ioctl_shared() failed, err = %d",
  1781. __func__, err);
  1782. kfree(user);
  1783. goto done;
  1784. }
  1785. /* Update size with actual payload size */
  1786. size = sizeof(userarg32) + user->payload_size;
  1787. if (!access_ok(arg, size)) {
  1788. dev_err(rtd->dev,
  1789. "%s: Failed to verify write, size = %d\n",
  1790. __func__, size);
  1791. err = -EFAULT;
  1792. kfree(user);
  1793. goto done;
  1794. }
  1795. user32 = kzalloc(size, GFP_KERNEL);
  1796. if (!user32) {
  1797. err = -ENOMEM;
  1798. kfree(user);
  1799. goto done;
  1800. }
  1801. user32->status = user->status;
  1802. user32->payload_size = user->payload_size;
  1803. memcpy(user32->payload, user->payload,
  1804. user32->payload_size);
  1805. if (copy_to_user(arg, user32, size)) {
  1806. dev_err(rtd->dev,
  1807. "%s: Failed to copy payload to user, size = %d",
  1808. __func__, size);
  1809. err = -EFAULT;
  1810. }
  1811. kfree(user);
  1812. kfree(user32);
  1813. break;
  1814. }
  1815. case SNDRV_LSM_EVENT_STATUS_V3_32: {
  1816. struct snd_lsm_event_status_v3_32 userarg32, *user32 = NULL;
  1817. struct snd_lsm_event_status_v3 *user = NULL;
  1818. if (prtd->lsm_client->event_type !=
  1819. LSM_DET_EVENT_TYPE_LEGACY) {
  1820. dev_err(rtd->dev,
  1821. "%s: %s: Invalid event request\n",
  1822. __func__, "SNDRV_LSM_EVENT_STATUS_V3_32");
  1823. err = -EINVAL;
  1824. goto done;
  1825. }
  1826. if (copy_from_user(&userarg32, arg, sizeof(userarg32))) {
  1827. dev_err(rtd->dev, "%s: err copyuser ioctl %s\n",
  1828. __func__, "SNDRV_LSM_EVENT_STATUS_V3_32");
  1829. err = -EFAULT;
  1830. goto done;
  1831. }
  1832. if (userarg32.payload_size >
  1833. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  1834. pr_err("%s: payload_size %d is invalid, max allowed = %d\n",
  1835. __func__, userarg32.payload_size,
  1836. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  1837. err = -EINVAL;
  1838. goto done;
  1839. }
  1840. size = sizeof(*user) + userarg32.payload_size;
  1841. user = kzalloc(size, GFP_KERNEL);
  1842. if (!user) {
  1843. dev_err(rtd->dev,
  1844. "%s: Allocation failed event status size %d\n",
  1845. __func__, size);
  1846. err = -ENOMEM;
  1847. goto done;
  1848. }
  1849. cmd = SNDRV_LSM_EVENT_STATUS_V3;
  1850. user->payload_size = userarg32.payload_size;
  1851. err = msm_lsm_ioctl_shared(substream, cmd, user);
  1852. /* Update size with actual payload size */
  1853. size = sizeof(userarg32) + user->payload_size;
  1854. if (!err && !access_ok(arg, size)) {
  1855. dev_err(rtd->dev,
  1856. "%s: write verify failed size %d\n",
  1857. __func__, size);
  1858. err = -EFAULT;
  1859. }
  1860. if (!err) {
  1861. user32 = kzalloc(size, GFP_KERNEL);
  1862. if (!user32) {
  1863. dev_err(rtd->dev,
  1864. "%s: Allocation event user status size %d\n",
  1865. __func__, size);
  1866. err = -EFAULT;
  1867. } else {
  1868. user32->timestamp_lsw = user->timestamp_lsw;
  1869. user32->timestamp_msw = user->timestamp_msw;
  1870. user32->status = user->status;
  1871. user32->payload_size = user->payload_size;
  1872. memcpy(user32->payload,
  1873. user->payload, user32->payload_size);
  1874. }
  1875. }
  1876. if (!err && (copy_to_user(arg, user32, size))) {
  1877. dev_err(rtd->dev, "%s: failed to copy payload %d",
  1878. __func__, size);
  1879. err = -EFAULT;
  1880. }
  1881. kfree(user);
  1882. kfree(user32);
  1883. if (err)
  1884. dev_err(rtd->dev, "%s: lsmevent failed %d",
  1885. __func__, err);
  1886. break;
  1887. }
  1888. case SNDRV_LSM_REG_SND_MODEL_V2_32: {
  1889. struct snd_lsm_sound_model_v2_32 snd_modelv232;
  1890. struct snd_lsm_sound_model_v2 snd_modelv2;
  1891. if (prtd->lsm_client->use_topology) {
  1892. dev_err(rtd->dev,
  1893. "%s: %s: not supported if using topology\n",
  1894. __func__, "REG_SND_MODEL_V2");
  1895. err = -EINVAL;
  1896. goto done;
  1897. }
  1898. if (copy_from_user(&snd_modelv232, arg,
  1899. sizeof(snd_modelv232))) {
  1900. err = -EFAULT;
  1901. dev_err(rtd->dev,
  1902. "%s: copy user failed, size %zd %s\n",
  1903. __func__,
  1904. sizeof(struct snd_lsm_sound_model_v2_32),
  1905. "SNDRV_LSM_REG_SND_MODEL_V2_32");
  1906. } else {
  1907. snd_modelv2.confidence_level =
  1908. compat_ptr(snd_modelv232.confidence_level);
  1909. snd_modelv2.data = compat_ptr(snd_modelv232.data);
  1910. snd_modelv2.data_size = snd_modelv232.data_size;
  1911. snd_modelv2.detect_failure =
  1912. snd_modelv232.detect_failure;
  1913. snd_modelv2.detection_mode =
  1914. snd_modelv232.detection_mode;
  1915. snd_modelv2.num_confidence_levels =
  1916. snd_modelv232.num_confidence_levels;
  1917. cmd = SNDRV_LSM_REG_SND_MODEL_V2;
  1918. err = msm_lsm_ioctl_shared(substream, cmd,
  1919. &snd_modelv2);
  1920. if (err)
  1921. dev_err(rtd->dev,
  1922. "%s: ioctl %s failed\n", __func__,
  1923. "SNDDRV_LSM_REG_SND_MODEL_V2_32");
  1924. }
  1925. break;
  1926. }
  1927. case SNDRV_LSM_SET_PARAMS_32:{
  1928. struct snd_lsm_detection_params_32 det_params32;
  1929. struct snd_lsm_detection_params det_params;
  1930. if (prtd->lsm_client->use_topology) {
  1931. dev_err(rtd->dev,
  1932. "%s: %s: not supported if using topology\n",
  1933. __func__, "SET_PARAMS_32");
  1934. err = -EINVAL;
  1935. }
  1936. if (copy_from_user(&det_params32, arg,
  1937. sizeof(det_params32))) {
  1938. err = -EFAULT;
  1939. dev_err(rtd->dev,
  1940. "%s: %s: copy_from_user failed, size = %zd\n",
  1941. __func__, "SNDRV_LSM_SET_PARAMS_32",
  1942. sizeof(det_params32));
  1943. } else {
  1944. det_params.conf_level =
  1945. compat_ptr(det_params32.conf_level);
  1946. det_params.detect_mode =
  1947. det_params32.detect_mode;
  1948. det_params.num_confidence_levels =
  1949. det_params32.num_confidence_levels;
  1950. det_params.detect_failure =
  1951. det_params32.detect_failure;
  1952. cmd = SNDRV_LSM_SET_PARAMS;
  1953. err = msm_lsm_ioctl_shared(substream, cmd,
  1954. &det_params);
  1955. if (err)
  1956. dev_err(rtd->dev,
  1957. "%s: ioctl %s failed\n", __func__,
  1958. "SNDRV_LSM_SET_PARAMS");
  1959. }
  1960. break;
  1961. }
  1962. case SNDRV_LSM_SET_MODULE_PARAMS_32:
  1963. case SNDRV_LSM_SET_MODULE_PARAMS_V2_32: {
  1964. struct snd_lsm_module_params_32 p_data_32;
  1965. struct snd_lsm_module_params p_data;
  1966. u8 *params32;
  1967. size_t expected_size = 0, count;
  1968. struct lsm_params_info_32 *p_info_32 = NULL;
  1969. struct lsm_params_info_v2_32 *p_info_v2_32 = NULL;
  1970. struct lsm_params_info_v2 p_info;
  1971. if (!prtd->lsm_client->use_topology) {
  1972. dev_err(rtd->dev,
  1973. "%s: %s: not supported if not using topology\n",
  1974. __func__, "SET_MODULE_PARAMS(_V2)_32");
  1975. err = -EINVAL;
  1976. goto done;
  1977. }
  1978. if (copy_from_user(&p_data_32, arg,
  1979. sizeof(p_data_32))) {
  1980. dev_err(rtd->dev,
  1981. "%s: %s: copy_from_user failed, size = %zd\n",
  1982. __func__, "SET_MODULE_PARAMS(_V2)_32",
  1983. sizeof(p_data_32));
  1984. err = -EFAULT;
  1985. goto done;
  1986. }
  1987. p_data.params = compat_ptr(p_data_32.params);
  1988. p_data.num_params = p_data_32.num_params;
  1989. p_data.data_size = p_data_32.data_size;
  1990. if (p_data.num_params > LSM_PARAMS_MAX) {
  1991. dev_err(rtd->dev,
  1992. "%s: %s: Invalid num_params %d\n",
  1993. __func__, "SET_MODULE_PARAMS(_V2)_32",
  1994. p_data.num_params);
  1995. err = -EINVAL;
  1996. goto done;
  1997. }
  1998. expected_size = (cmd == SNDRV_LSM_SET_MODULE_PARAMS_32) ?
  1999. p_data.num_params * sizeof(struct lsm_params_info_32) :
  2000. p_data.num_params * sizeof(struct lsm_params_info_v2_32);
  2001. if (p_data.data_size != expected_size) {
  2002. dev_err(rtd->dev,
  2003. "%s: %s: Invalid size %d, expected_size %d\n",
  2004. __func__, "SET_MODULE_PARAMS(_V2)_32",
  2005. p_data.data_size, expected_size);
  2006. err = -EINVAL;
  2007. goto done;
  2008. }
  2009. params32 = kzalloc(p_data.data_size, GFP_KERNEL);
  2010. if (!params32) {
  2011. err = -ENOMEM;
  2012. goto done;
  2013. }
  2014. if (copy_from_user(params32, p_data.params,
  2015. p_data.data_size)) {
  2016. dev_err(rtd->dev,
  2017. "%s: %s: copy_from_user failed, size = %d\n",
  2018. __func__, "params32", p_data.data_size);
  2019. kfree(params32);
  2020. err = -EFAULT;
  2021. goto done;
  2022. }
  2023. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS_32)
  2024. p_info_32 = (struct lsm_params_info_32 *) params32;
  2025. else
  2026. p_info_v2_32 = (struct lsm_params_info_v2_32 *) params32;
  2027. for (count = 0; count < p_data.num_params; count++) {
  2028. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS_32) {
  2029. p_info.module_id = p_info_32->module_id;
  2030. p_info.param_id = p_info_32->param_id;
  2031. p_info.param_size = p_info_32->param_size;
  2032. p_info.param_data = compat_ptr(p_info_32->param_data);
  2033. p_info.param_type = p_info_32->param_type;
  2034. p_info.instance_id = INSTANCE_ID_0;
  2035. p_info.stage_idx = LSM_STAGE_INDEX_FIRST;
  2036. p_info.model_id = 0;
  2037. p_info_32++;
  2038. } else {
  2039. p_info.module_id = p_info_v2_32->module_id;
  2040. p_info.param_id = p_info_v2_32->param_id;
  2041. p_info.param_size = p_info_v2_32->param_size;
  2042. p_info.param_data = compat_ptr(p_info_v2_32->param_data);
  2043. p_info.param_type = p_info_v2_32->param_type;
  2044. p_info.instance_id = p_info_v2_32->instance_id;
  2045. p_info.stage_idx = p_info_v2_32->stage_idx;
  2046. /* set sound model id to 0 for backward compatibility */
  2047. p_info.model_id = 0;
  2048. if (LSM_REG_MULTI_SND_MODEL == p_info_v2_32->param_type ||
  2049. LSM_DEREG_MULTI_SND_MODEL == p_info_v2_32->param_type ||
  2050. LSM_MULTI_SND_MODEL_CONFIDENCE_LEVELS ==
  2051. p_info_v2_32->param_type)
  2052. p_info.model_id = p_info_v2_32->model_id;
  2053. p_info_v2_32++;
  2054. }
  2055. err = msm_lsm_process_params(substream, &p_info);
  2056. if (err)
  2057. dev_err(rtd->dev,
  2058. "%s: Failed to process param, type%d stage=%d err=%d\n",
  2059. __func__, p_info.param_type, p_info.stage_idx, err);
  2060. }
  2061. kfree(params32);
  2062. break;
  2063. }
  2064. case SNDRV_LSM_REG_SND_MODEL_V2:
  2065. case SNDRV_LSM_SET_PARAMS:
  2066. case SNDRV_LSM_SET_MODULE_PARAMS:
  2067. case SNDRV_LSM_SET_MODULE_PARAMS_V2:
  2068. /*
  2069. * In ideal cases, the compat_ioctl should never be called
  2070. * with the above unlocked ioctl commands. Print error
  2071. * and return error if it does.
  2072. */
  2073. dev_err(rtd->dev,
  2074. "%s: Invalid cmd for compat_ioctl\n",
  2075. __func__);
  2076. err = -EINVAL;
  2077. break;
  2078. default:
  2079. err = msm_lsm_ioctl_shared(substream, cmd, arg);
  2080. break;
  2081. }
  2082. done:
  2083. mutex_unlock(&prtd->lsm_api_lock);
  2084. return err;
  2085. }
  2086. #endif /* CONFIG_AUDIO_QGKI */
  2087. #else
  2088. #define msm_lsm_ioctl_compat NULL
  2089. #endif
  2090. static int msm_lsm_ioctl(struct snd_pcm_substream *substream,
  2091. unsigned int cmd, void __user *arg)
  2092. {
  2093. int err = 0;
  2094. u32 size = 0;
  2095. struct snd_pcm_runtime *runtime;
  2096. struct snd_soc_pcm_runtime *rtd;
  2097. struct lsm_priv *prtd;
  2098. if (!substream || !substream->private_data) {
  2099. pr_err("%s: Invalid %s\n", __func__,
  2100. (!substream) ? "substream" : "private_data");
  2101. return -EINVAL;
  2102. }
  2103. runtime = substream->runtime;
  2104. prtd = runtime->private_data;
  2105. rtd = substream->private_data;
  2106. mutex_lock(&prtd->lsm_api_lock);
  2107. switch (cmd) {
  2108. case SNDRV_LSM_REG_SND_MODEL_V2: {
  2109. struct snd_lsm_sound_model_v2 snd_model_v2;
  2110. if (prtd->lsm_client->use_topology) {
  2111. dev_err(rtd->dev,
  2112. "%s: %s: not supported if using topology\n",
  2113. __func__, "REG_SND_MODEL_V2");
  2114. err = -EINVAL;
  2115. goto done;
  2116. }
  2117. if (copy_from_user(&snd_model_v2, arg, sizeof(snd_model_v2))) {
  2118. err = -EFAULT;
  2119. dev_err(rtd->dev,
  2120. "%s: copy from user failed, size %zd\n",
  2121. __func__,
  2122. sizeof(struct snd_lsm_sound_model_v2));
  2123. }
  2124. if (!err)
  2125. err = msm_lsm_ioctl_shared(substream, cmd,
  2126. &snd_model_v2);
  2127. if (err)
  2128. dev_err(rtd->dev,
  2129. "%s REG_SND_MODEL failed err %d\n",
  2130. __func__, err);
  2131. goto done;
  2132. }
  2133. break;
  2134. case SNDRV_LSM_SET_PARAMS: {
  2135. struct snd_lsm_detection_params det_params;
  2136. if (prtd->lsm_client->use_topology) {
  2137. dev_err(rtd->dev,
  2138. "%s: %s: not supported if using topology\n",
  2139. __func__, "SET_PARAMS");
  2140. err = -EINVAL;
  2141. goto done;
  2142. }
  2143. pr_debug("%s: SNDRV_LSM_SET_PARAMS\n", __func__);
  2144. if (copy_from_user(&det_params, arg,
  2145. sizeof(det_params))) {
  2146. dev_err(rtd->dev,
  2147. "%s: %s: copy_from_user failed, size %zd\n",
  2148. __func__, "SNDRV_LSM_SET_PARAMS",
  2149. sizeof(det_params));
  2150. err = -EFAULT;
  2151. }
  2152. if (!err)
  2153. err = msm_lsm_ioctl_shared(substream, cmd,
  2154. &det_params);
  2155. else
  2156. dev_err(rtd->dev,
  2157. "%s: LSM_SET_PARAMS failed, err %d\n",
  2158. __func__, err);
  2159. goto done;
  2160. }
  2161. case SNDRV_LSM_SET_MODULE_PARAMS:
  2162. case SNDRV_LSM_SET_MODULE_PARAMS_V2: {
  2163. struct snd_lsm_module_params p_data;
  2164. struct lsm_params_info *temp_ptr_info = NULL;
  2165. struct lsm_params_info_v2 info_v2;
  2166. struct lsm_params_info_v2 *ptr_info_v2 = NULL, *temp_ptr_info_v2 = NULL;
  2167. size_t p_size = 0, count;
  2168. u8 *params;
  2169. memset(&info_v2, 0, sizeof(info_v2));
  2170. if (!prtd->lsm_client->use_topology) {
  2171. dev_err(rtd->dev,
  2172. "%s: %s: not supported if not using topology\n",
  2173. __func__, "SET_MODULE_PARAMS(_V2)");
  2174. err = -EINVAL;
  2175. goto done;
  2176. }
  2177. if (copy_from_user(&p_data, arg,
  2178. sizeof(p_data))) {
  2179. dev_err(rtd->dev,
  2180. "%s: %s: copy_from_user failed, size = %zd\n",
  2181. __func__, "p_data", sizeof(p_data));
  2182. err = -EFAULT;
  2183. goto done;
  2184. }
  2185. if (p_data.num_params > LSM_PARAMS_MAX) {
  2186. dev_err(rtd->dev,
  2187. "%s: %s: Invalid num_params %d\n",
  2188. __func__, "SET_MODULE_PARAMS(_V2)",
  2189. p_data.num_params);
  2190. err = -EINVAL;
  2191. goto done;
  2192. }
  2193. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS)
  2194. p_size = p_data.num_params * sizeof(struct lsm_params_info);
  2195. else
  2196. p_size = p_data.num_params * sizeof(struct lsm_params_info_v2);
  2197. if (p_data.data_size != p_size) {
  2198. dev_err(rtd->dev,
  2199. "%s: %s: Invalid data_size(%u) against expected(%zd)\n",
  2200. __func__, "SET_MODULE_PARAMS(_V2)",
  2201. p_data.data_size, p_size);
  2202. err = -EFAULT;
  2203. goto done;
  2204. }
  2205. params = kzalloc(p_size, GFP_KERNEL);
  2206. if (!params) {
  2207. err = -ENOMEM;
  2208. goto done;
  2209. }
  2210. if (copy_from_user(params, p_data.params,
  2211. p_data.data_size)) {
  2212. dev_err(rtd->dev,
  2213. "%s: %s: copy_from_user failed, size = %d\n",
  2214. __func__, "set module params", p_data.data_size);
  2215. kfree(params);
  2216. err = -EFAULT;
  2217. goto done;
  2218. }
  2219. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS)
  2220. temp_ptr_info = (struct lsm_params_info *)params;
  2221. else
  2222. temp_ptr_info_v2 = (struct lsm_params_info_v2 *)params;
  2223. for (count = 0; count < p_data.num_params; count++) {
  2224. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS) {
  2225. /* convert to V2 param info struct from legacy param info */
  2226. info_v2.module_id = temp_ptr_info->module_id;
  2227. info_v2.param_id = temp_ptr_info->param_id;
  2228. info_v2.param_size = temp_ptr_info->param_size;
  2229. info_v2.param_data = temp_ptr_info->param_data;
  2230. info_v2.param_type = temp_ptr_info->param_type;
  2231. info_v2.instance_id = INSTANCE_ID_0;
  2232. info_v2.stage_idx = LSM_STAGE_INDEX_FIRST;
  2233. info_v2.model_id = 0;
  2234. ptr_info_v2 = &info_v2;
  2235. temp_ptr_info++;
  2236. } else {
  2237. if (LSM_REG_MULTI_SND_MODEL != temp_ptr_info_v2->param_type ||
  2238. LSM_DEREG_MULTI_SND_MODEL !=
  2239. temp_ptr_info_v2->param_type ||
  2240. LSM_MULTI_SND_MODEL_CONFIDENCE_LEVELS !=
  2241. temp_ptr_info_v2->param_type) {
  2242. /* set sound model id to 0 for backward compatibility */
  2243. temp_ptr_info_v2->model_id = 0;
  2244. }
  2245. /*
  2246. * Just copy the pointer as user
  2247. * already provided sound model id
  2248. */
  2249. ptr_info_v2 = temp_ptr_info_v2;
  2250. temp_ptr_info_v2++;
  2251. }
  2252. err = msm_lsm_process_params(substream, ptr_info_v2);
  2253. if (err)
  2254. dev_err(rtd->dev,
  2255. "%s: Failed to process param, type=%d stage=%d err=%d\n",
  2256. __func__, ptr_info_v2->param_type,
  2257. ptr_info_v2->stage_idx, err);
  2258. }
  2259. kfree(params);
  2260. break;
  2261. }
  2262. case SNDRV_LSM_EVENT_STATUS:
  2263. case SNDRV_LSM_GENERIC_DET_EVENT: {
  2264. struct snd_lsm_event_status *user = NULL;
  2265. struct snd_lsm_event_status userarg;
  2266. dev_dbg(rtd->dev,
  2267. "%s: %s\n", __func__,
  2268. (cmd == SNDRV_LSM_EVENT_STATUS) ?
  2269. "SNDRV_LSM_EVENT_STATUS" :
  2270. "SNDRV_LSM_GENERIC_DET_EVENT");
  2271. err = msm_lsm_check_event_type(prtd->lsm_client, cmd);
  2272. if (err)
  2273. goto done;
  2274. if (copy_from_user(&userarg, arg, sizeof(userarg))) {
  2275. dev_err(rtd->dev,
  2276. "%s: %s: Copy from user failed\n", __func__,
  2277. (cmd == SNDRV_LSM_EVENT_STATUS) ?
  2278. "SNDRV_LSM_EVENT_STATUS" :
  2279. "SNDRV_LSM_GENERIC_DET_EVENT");
  2280. err = -EFAULT;
  2281. goto done;
  2282. }
  2283. if (userarg.payload_size >
  2284. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  2285. dev_err(rtd->dev,
  2286. "%s: payload_size %d is invalid, max allowed = %d\n",
  2287. __func__, userarg.payload_size,
  2288. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  2289. err = -EINVAL;
  2290. goto done;
  2291. }
  2292. size = sizeof(struct snd_lsm_event_status) +
  2293. userarg.payload_size;
  2294. user = kzalloc(size, GFP_KERNEL);
  2295. if (!user) {
  2296. err = -ENOMEM;
  2297. goto done;
  2298. }
  2299. user->payload_size = userarg.payload_size;
  2300. err = msm_lsm_ioctl_shared(substream, cmd, user);
  2301. if (err) {
  2302. dev_err(rtd->dev,
  2303. "%s: msm_lsm_ioctl_shared() failed, err = %d",
  2304. __func__, err);
  2305. kfree(user);
  2306. goto done;
  2307. }
  2308. /* Update size with actual payload size */
  2309. size = sizeof(*user) + user->payload_size;
  2310. if (!access_ok(arg, size)) {
  2311. dev_err(rtd->dev,
  2312. "%s: Failed to verify write, size = %d\n",
  2313. __func__, size);
  2314. err = -EFAULT;
  2315. }
  2316. if (!err && copy_to_user(arg, user, size)) {
  2317. dev_err(rtd->dev,
  2318. "%s: Failed to copy payload to user, size = %d\n",
  2319. __func__, size);
  2320. err = -EFAULT;
  2321. }
  2322. kfree(user);
  2323. break;
  2324. }
  2325. case SNDRV_LSM_EVENT_STATUS_V3: {
  2326. struct snd_lsm_event_status_v3 *user = NULL;
  2327. struct snd_lsm_event_status_v3 userarg;
  2328. dev_dbg(rtd->dev,
  2329. "%s: SNDRV_LSM_EVENT_STATUS_V3\n", __func__);
  2330. if (prtd->lsm_client->event_type !=
  2331. LSM_DET_EVENT_TYPE_LEGACY) {
  2332. dev_err(rtd->dev,
  2333. "%s: %s: Invalid event request\n",
  2334. __func__, "SNDRV_LSM_EVENT_STATUS_V3");
  2335. err = -EINVAL;
  2336. goto done;
  2337. }
  2338. if (!arg) {
  2339. dev_err(rtd->dev,
  2340. "%s: Invalid params event_status_v3\n",
  2341. __func__);
  2342. err = -EINVAL;
  2343. goto done;
  2344. }
  2345. if (copy_from_user(&userarg, arg, sizeof(userarg))) {
  2346. dev_err(rtd->dev,
  2347. "%s: err copyuser event_status_v3\n",
  2348. __func__);
  2349. err = -EFAULT;
  2350. goto done;
  2351. }
  2352. if (userarg.payload_size >
  2353. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  2354. pr_err("%s: payload_size %d is invalid, max allowed = %d\n",
  2355. __func__, userarg.payload_size,
  2356. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  2357. err = -EINVAL;
  2358. goto done;
  2359. }
  2360. size = sizeof(struct snd_lsm_event_status_v3) +
  2361. userarg.payload_size;
  2362. user = kzalloc(size, GFP_KERNEL);
  2363. if (!user) {
  2364. dev_err(rtd->dev,
  2365. "%s: Allocation failed event status size %d\n",
  2366. __func__, size);
  2367. err = -EFAULT;
  2368. goto done;
  2369. }
  2370. user->payload_size = userarg.payload_size;
  2371. err = msm_lsm_ioctl_shared(substream, cmd, user);
  2372. /* Update size with actual payload size */
  2373. size = sizeof(*user) + user->payload_size;
  2374. if (!err && !access_ok(arg, size)) {
  2375. dev_err(rtd->dev,
  2376. "%s: write verify failed size %d\n",
  2377. __func__, size);
  2378. err = -EFAULT;
  2379. }
  2380. if (!err && (copy_to_user(arg, user, size))) {
  2381. dev_err(rtd->dev,
  2382. "%s: failed to copy payload %d",
  2383. __func__, size);
  2384. err = -EFAULT;
  2385. }
  2386. kfree(user);
  2387. if (err)
  2388. dev_err(rtd->dev,
  2389. "%s: lsm_event_v3 failed %d", __func__, err);
  2390. break;
  2391. }
  2392. default:
  2393. err = msm_lsm_ioctl_shared(substream, cmd, arg);
  2394. break;
  2395. }
  2396. done:
  2397. mutex_unlock(&prtd->lsm_api_lock);
  2398. return err;
  2399. }
  2400. static int msm_lsm_open(struct snd_pcm_substream *substream)
  2401. {
  2402. struct snd_pcm_runtime *runtime = substream->runtime;
  2403. struct lsm_priv *prtd;
  2404. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2405. int ret = 0, i;
  2406. pr_debug("%s\n", __func__);
  2407. prtd = kzalloc(sizeof(struct lsm_priv), GFP_KERNEL);
  2408. if (!prtd) {
  2409. pr_err("%s: Failed to allocate memory for lsm_priv\n",
  2410. __func__);
  2411. return -ENOMEM;
  2412. }
  2413. mutex_init(&prtd->lsm_api_lock);
  2414. spin_lock_init(&prtd->event_lock);
  2415. spin_lock_init(&prtd->xrun_lock);
  2416. init_waitqueue_head(&prtd->event_wait);
  2417. init_waitqueue_head(&prtd->period_wait);
  2418. prtd->substream = substream;
  2419. runtime->private_data = prtd;
  2420. runtime->hw = msm_pcm_hardware_capture;
  2421. ret = snd_pcm_hw_constraint_list(runtime, 0,
  2422. SNDRV_PCM_HW_PARAM_RATE,
  2423. &constraints_sample_rates);
  2424. if (ret < 0)
  2425. pr_info("%s: snd_pcm_hw_constraint_list failed ret %d\n",
  2426. __func__, ret);
  2427. /* Ensure that buffer size is a multiple of period size */
  2428. ret = snd_pcm_hw_constraint_integer(runtime,
  2429. SNDRV_PCM_HW_PARAM_PERIODS);
  2430. if (ret < 0)
  2431. pr_info("%s: snd_pcm_hw_constraint_integer failed ret %d\n",
  2432. __func__, ret);
  2433. ret = snd_pcm_hw_constraint_minmax(runtime,
  2434. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  2435. CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  2436. CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  2437. if (ret < 0)
  2438. pr_info("%s: constraint for buffer bytes min max ret = %d\n",
  2439. __func__, ret);
  2440. ret = snd_pcm_hw_constraint_step(runtime, 0,
  2441. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  2442. if (ret < 0) {
  2443. pr_info("%s: constraint for period bytes step ret = %d\n",
  2444. __func__, ret);
  2445. }
  2446. ret = snd_pcm_hw_constraint_step(runtime, 0,
  2447. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  2448. if (ret < 0)
  2449. pr_info("%s: constraint for buffer bytes step ret = %d\n",
  2450. __func__, ret);
  2451. prtd->lsm_client = q6lsm_client_alloc(
  2452. lsm_event_handler, prtd);
  2453. if (!prtd->lsm_client) {
  2454. pr_err("%s: Could not allocate memory\n", __func__);
  2455. kfree(prtd);
  2456. runtime->private_data = NULL;
  2457. return -ENOMEM;
  2458. }
  2459. prtd->lsm_client->opened = false;
  2460. prtd->lsm_client->started = false;
  2461. prtd->lsm_client->session_state = IDLE;
  2462. prtd->lsm_client->poll_enable = true;
  2463. prtd->lsm_client->perf_mode = 0;
  2464. prtd->lsm_client->event_mode = LSM_EVENT_NON_TIME_STAMP_MODE;
  2465. prtd->lsm_client->event_type = LSM_DET_EVENT_TYPE_LEGACY;
  2466. prtd->lsm_client->fe_id = rtd->dai_link->id;
  2467. prtd->lsm_client->unprocessed_data = 0;
  2468. prtd->lsm_client->num_sound_models = 0;
  2469. prtd->lsm_client->num_keywords = 0;
  2470. prtd->lsm_client->multi_snd_model_confidence_levels = NULL;
  2471. for (i = 0; i < LSM_MAX_STAGES_PER_SESSION; i++)
  2472. INIT_LIST_HEAD(&prtd->lsm_client->stage_cfg[i].sound_models);
  2473. prtd->ws = wakeup_source_register(rtd->dev, "lsm-client");
  2474. return 0;
  2475. }
  2476. static int msm_lsm_send_ch_mix_config(struct snd_pcm_substream *substream)
  2477. {
  2478. struct snd_pcm_runtime *runtime = substream->runtime;
  2479. struct lsm_priv *prtd = runtime->private_data;
  2480. struct snd_soc_pcm_runtime *rtd;
  2481. struct lsm_hw_params *in_params;
  2482. int pp_ch_cnt;
  2483. int *ch_wght_coeff;
  2484. int ret = 0, i, idx;
  2485. /*
  2486. * The output channels from channel mixer is the input to LSM (stream)
  2487. * side and is read from in_params->num_chs.
  2488. *
  2489. * The input channels to channel mixer are the output channels from
  2490. * the device side (routing) and is obtained by reading the
  2491. * pp_ch_cnt.
  2492. *
  2493. * For LSM to be functional, only unity channel mixing is allowed.
  2494. */
  2495. in_params = &prtd->lsm_client->in_hw_params;
  2496. rtd = prtd->substream->private_data;
  2497. pp_ch_cnt = msm_pcm_routing_get_pp_ch_cnt(rtd->dai_link->id,
  2498. SESSION_TYPE_TX);
  2499. if (pp_ch_cnt < 0 ||
  2500. pp_ch_cnt > LSM_V3P0_MAX_NUM_CHANNELS ||
  2501. in_params->num_chs > LSM_V3P0_MAX_NUM_CHANNELS) {
  2502. dev_err(rtd->dev,
  2503. "%s: invalid ch cnt, pp_ch_cnt %d in_ch_cnt %d\n",
  2504. __func__, pp_ch_cnt, in_params->num_chs);
  2505. return -EINVAL;
  2506. }
  2507. if (!pp_ch_cnt ||
  2508. (pp_ch_cnt == in_params->num_chs)) {
  2509. dev_dbg(rtd->dev,
  2510. "%s: Skip ch mixing, pp_ch_cnt %d in_ch_cnt %d\n",
  2511. __func__, pp_ch_cnt, in_params->num_chs);
  2512. return 0;
  2513. }
  2514. ch_wght_coeff = kzalloc(in_params->num_chs * pp_ch_cnt * sizeof(int),
  2515. GFP_KERNEL);
  2516. if (!ch_wght_coeff)
  2517. return -ENOMEM;
  2518. /*
  2519. * channel weight co-efficients is a m X n array, where
  2520. * m = number of input channels to ch mixer (pp_ch_cnt)
  2521. * n = number of output channels from ch mixer (in_params->num_chs)
  2522. */
  2523. for (i = 0; i < in_params->num_chs; i++) {
  2524. idx = (i * pp_ch_cnt) + i;
  2525. ch_wght_coeff[idx] = 1;
  2526. }
  2527. ret = msm_pcm_routing_send_chmix_cfg(rtd->dai_link->id,
  2528. pp_ch_cnt, in_params->num_chs,
  2529. ch_wght_coeff,
  2530. SESSION_TYPE_TX, STREAM_TYPE_LSM);
  2531. if (ret)
  2532. dev_err(rtd->dev,
  2533. "%s: Failed to configure channel mixer err %d\n",
  2534. __func__, ret);
  2535. kfree(ch_wght_coeff);
  2536. return ret;
  2537. }
  2538. static int msm_lsm_prepare(struct snd_pcm_substream *substream)
  2539. {
  2540. struct snd_pcm_runtime *runtime = substream->runtime;
  2541. struct lsm_priv *prtd = runtime->private_data;
  2542. struct snd_soc_pcm_runtime *rtd;
  2543. int ret = 0;
  2544. if (!substream->private_data) {
  2545. pr_err("%s: Invalid private_data", __func__);
  2546. return -EINVAL;
  2547. }
  2548. rtd = prtd->substream->private_data;
  2549. if (!prtd->lsm_client) {
  2550. dev_err(rtd->dev,
  2551. "%s: LSM client data ptr is NULL\n", __func__);
  2552. return -EINVAL;
  2553. }
  2554. if (q6lsm_set_media_fmt_v2_params(prtd->lsm_client))
  2555. dev_dbg(rtd->dev,
  2556. "%s: failed to set lsm media fmt params\n", __func__);
  2557. if (prtd->lsm_client->session_state == IDLE) {
  2558. ret = msm_pcm_routing_reg_phy_compr_stream(
  2559. rtd->dai_link->id,
  2560. prtd->lsm_client->perf_mode,
  2561. prtd->lsm_client->session,
  2562. SNDRV_PCM_STREAM_CAPTURE,
  2563. LISTEN);
  2564. if (ret) {
  2565. dev_err(rtd->dev,
  2566. "%s: register phy compr stream failed %d\n",
  2567. __func__, ret);
  2568. return ret;
  2569. }
  2570. ret = msm_lsm_send_ch_mix_config(substream);
  2571. if (ret) {
  2572. msm_pcm_routing_dereg_phy_stream(rtd->dai_link->id,
  2573. SNDRV_PCM_STREAM_CAPTURE);
  2574. return ret;
  2575. }
  2576. }
  2577. prtd->lsm_client->session_state = RUNNING;
  2578. runtime->private_data = prtd;
  2579. return ret;
  2580. }
  2581. static int msm_lsm_close(struct snd_pcm_substream *substream)
  2582. {
  2583. unsigned long flags;
  2584. struct snd_pcm_runtime *runtime = substream->runtime;
  2585. struct lsm_priv *prtd = runtime->private_data;
  2586. struct snd_soc_pcm_runtime *rtd;
  2587. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  2588. int ret = 0;
  2589. int be_id = 0;
  2590. int fe_id = 0;
  2591. if (!substream->private_data) {
  2592. pr_err("%s: Invalid private_data", __func__);
  2593. return -EINVAL;
  2594. }
  2595. if (!prtd || !prtd->lsm_client) {
  2596. pr_err("%s: No LSM session active\n", __func__);
  2597. return -EINVAL;
  2598. }
  2599. rtd = substream->private_data;
  2600. dev_dbg(rtd->dev, "%s\n", __func__);
  2601. if (prtd->lsm_client->started) {
  2602. if (prtd->lsm_client->lab_enable) {
  2603. atomic_set(&prtd->read_abort, 1);
  2604. if (prtd->lsm_client->lab_started) {
  2605. ret = q6lsm_stop_lab(prtd->lsm_client);
  2606. if (ret)
  2607. dev_err(rtd->dev,
  2608. "%s: stop lab failed ret %d\n",
  2609. __func__, ret);
  2610. prtd->lsm_client->lab_started = false;
  2611. }
  2612. if (prtd->lsm_client->lab_buffer) {
  2613. ret = msm_lsm_lab_buffer_alloc(prtd,
  2614. LAB_BUFFER_DEALLOC);
  2615. if (ret)
  2616. dev_err(rtd->dev,
  2617. "%s: lab buffer dealloc failed ret %d\n",
  2618. __func__, ret);
  2619. }
  2620. }
  2621. if (!atomic_read(&prtd->read_abort)) {
  2622. dev_dbg(rtd->dev,
  2623. "%s: set read_abort to stop buffering\n", __func__);
  2624. atomic_set(&prtd->read_abort, 1);
  2625. }
  2626. ret = q6lsm_stop(prtd->lsm_client, true);
  2627. if (ret)
  2628. dev_err(rtd->dev,
  2629. "%s: session stop failed, err = %d\n",
  2630. __func__, ret);
  2631. else
  2632. dev_dbg(rtd->dev,
  2633. "%s: LSM client session stopped %d\n",
  2634. __func__, ret);
  2635. /*
  2636. * Go Ahead and try de-register sound model,
  2637. * even if stop failed
  2638. */
  2639. prtd->lsm_client->started = false;
  2640. ret = q6lsm_deregister_sound_model(prtd->lsm_client);
  2641. if (ret)
  2642. dev_err(rtd->dev,
  2643. "%s: dereg_snd_model failed, err = %d\n",
  2644. __func__, ret);
  2645. else
  2646. dev_dbg(rtd->dev, "%s: dereg_snd_model successful\n",
  2647. __func__);
  2648. }
  2649. msm_pcm_routing_dereg_phy_stream(rtd->dai_link->id,
  2650. SNDRV_PCM_STREAM_CAPTURE);
  2651. if (prtd->lsm_client->opened) {
  2652. if (!atomic_read(&prtd->read_abort)) {
  2653. dev_dbg(rtd->dev,
  2654. "%s: set read_abort to stop buffering\n", __func__);
  2655. atomic_set(&prtd->read_abort, 1);
  2656. }
  2657. q6lsm_close(prtd->lsm_client);
  2658. prtd->lsm_client->opened = false;
  2659. }
  2660. fe_id = prtd->lsm_client->fe_id;
  2661. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, SESSION_TYPE_TX,
  2662. &be_id, &cfg_data);
  2663. if (ret < 0)
  2664. dev_dbg(rtd->dev,
  2665. "%s: get stream app type cfg failed, err = %d\n",
  2666. __func__, ret);
  2667. /*
  2668. * be_id will be 0 in case of LSM directly connects to AFE due to
  2669. * last_be_id_configured[fedai_id][session_type] has not been updated.
  2670. * And then the cfg_data from wrong combination would be reset without
  2671. * this if check. We reset only if app_type, acdb_dev_id, and sample_rate
  2672. * are valid.
  2673. */
  2674. if (!cfg_data.app_type &&
  2675. !cfg_data.acdb_dev_id && !cfg_data.sample_rate) {
  2676. dev_dbg(rtd->dev, "%s: no need to reset app type configs\n",
  2677. __func__);
  2678. } else {
  2679. memset(&cfg_data, 0, sizeof(cfg_data));
  2680. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id,
  2681. SESSION_TYPE_TX,
  2682. be_id,
  2683. &cfg_data);
  2684. if (ret < 0)
  2685. dev_dbg(rtd->dev,
  2686. "%s: set stream app type cfg failed, err = %d\n",
  2687. __func__, ret);
  2688. }
  2689. q6lsm_client_free(prtd->lsm_client);
  2690. wakeup_source_unregister(prtd->ws);
  2691. spin_lock_irqsave(&prtd->event_lock, flags);
  2692. kfree(prtd->event_status);
  2693. prtd->event_status = NULL;
  2694. kfree(prtd->det_event);
  2695. prtd->det_event = NULL;
  2696. spin_unlock_irqrestore(&prtd->event_lock, flags);
  2697. mutex_destroy(&prtd->lsm_api_lock);
  2698. kfree(prtd);
  2699. runtime->private_data = NULL;
  2700. return 0;
  2701. }
  2702. static int msm_lsm_hw_params(struct snd_pcm_substream *substream,
  2703. struct snd_pcm_hw_params *params)
  2704. {
  2705. struct snd_pcm_runtime *runtime = substream->runtime;
  2706. struct lsm_priv *prtd = runtime->private_data;
  2707. struct lsm_hw_params *out_hw_params = NULL;
  2708. struct lsm_hw_params *in_hw_params = NULL;
  2709. struct snd_soc_pcm_runtime *rtd;
  2710. if (!substream->private_data) {
  2711. pr_err("%s: Invalid private_data", __func__);
  2712. return -EINVAL;
  2713. }
  2714. rtd = substream->private_data;
  2715. if (!prtd || !params) {
  2716. dev_err(rtd->dev,
  2717. "%s: invalid params prtd %pK params %pK",
  2718. __func__, prtd, params);
  2719. return -EINVAL;
  2720. }
  2721. in_hw_params = &prtd->lsm_client->in_hw_params;
  2722. out_hw_params = &prtd->lsm_client->out_hw_params;
  2723. out_hw_params->num_chs = params_channels(params);
  2724. out_hw_params->period_count = params_periods(params);
  2725. out_hw_params->sample_rate = params_rate(params);
  2726. if (((out_hw_params->sample_rate != 16000) &&
  2727. (out_hw_params->sample_rate != 48000)) ||
  2728. (out_hw_params->period_count == 0)) {
  2729. dev_err(rtd->dev,
  2730. "%s: Invalid Params sample rate %d period count %d\n",
  2731. __func__, out_hw_params->sample_rate,
  2732. out_hw_params->period_count);
  2733. return -EINVAL;
  2734. }
  2735. if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE) {
  2736. out_hw_params->sample_size = 16;
  2737. } else if (params_format(params) == SNDRV_PCM_FORMAT_S24_LE) {
  2738. out_hw_params->sample_size = 24;
  2739. } else {
  2740. dev_err(rtd->dev, "%s: Invalid Format 0x%x\n",
  2741. __func__, params_format(params));
  2742. return -EINVAL;
  2743. }
  2744. out_hw_params->buf_sz = params_buffer_bytes(params) /
  2745. out_hw_params->period_count;
  2746. dev_dbg(rtd->dev,
  2747. "%s: channels %d sample rate %d sample size %d buffer size %d period count %d\n",
  2748. __func__, out_hw_params->num_chs, out_hw_params->sample_rate,
  2749. out_hw_params->sample_size, out_hw_params->buf_sz,
  2750. out_hw_params->period_count);
  2751. /*
  2752. * copy the out_hw_params to in_hw_params. in_hw_params will be
  2753. * over-written with LSM_SET_INPUT_HW_PARAMS ioctl from userspace.
  2754. * If this ioctl is not set, then it is assumed that input and
  2755. * output hw params for LSM are the same.
  2756. * Currently the period_count and buf_sz are unused for input params.
  2757. */
  2758. memcpy(in_hw_params, out_hw_params,
  2759. sizeof(struct lsm_hw_params));
  2760. return 0;
  2761. }
  2762. static snd_pcm_uframes_t msm_lsm_pcm_pointer(
  2763. struct snd_pcm_substream *substream)
  2764. {
  2765. struct snd_pcm_runtime *runtime = substream->runtime;
  2766. struct lsm_priv *prtd = runtime->private_data;
  2767. struct snd_soc_pcm_runtime *rtd;
  2768. if (!substream->private_data) {
  2769. pr_err("%s: Invalid private_data", __func__);
  2770. return -EINVAL;
  2771. }
  2772. rtd = substream->private_data;
  2773. if (!prtd) {
  2774. dev_err(rtd->dev,
  2775. "%s: Invalid param %pK\n", __func__, prtd);
  2776. return 0;
  2777. }
  2778. if (prtd->dma_write >= snd_pcm_lib_buffer_bytes(substream))
  2779. prtd->dma_write = 0;
  2780. dev_dbg(rtd->dev,
  2781. "%s: dma post = %d\n", __func__, prtd->dma_write);
  2782. return bytes_to_frames(runtime, prtd->dma_write);
  2783. }
  2784. static int msm_lsm_pcm_copy(struct snd_pcm_substream *substream, int ch,
  2785. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  2786. {
  2787. struct snd_pcm_runtime *runtime = substream->runtime;
  2788. struct lsm_priv *prtd = runtime->private_data;
  2789. char *pcm_buf = NULL;
  2790. int rc = 0, buf_index = 0;
  2791. unsigned long flags = 0;
  2792. struct snd_soc_pcm_runtime *rtd;
  2793. if (!substream->private_data) {
  2794. pr_err("%s: Invalid private_data", __func__);
  2795. return -EINVAL;
  2796. }
  2797. rtd = substream->private_data;
  2798. if (!prtd) {
  2799. dev_err(rtd->dev,
  2800. "%s: Invalid param %pK\n", __func__, prtd);
  2801. return -EINVAL;
  2802. }
  2803. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  2804. runtime->status->state == SNDRV_PCM_STATE_PREPARED) {
  2805. dev_err(rtd->dev,
  2806. "%s: runtime state incorrect %d", __func__,
  2807. runtime->status->state);
  2808. return 0;
  2809. }
  2810. rc = wait_event_timeout(prtd->period_wait,
  2811. (atomic_read(&prtd->buf_count) |
  2812. atomic_read(&prtd->read_abort)), (2 * HZ));
  2813. if (!rc) {
  2814. dev_err(rtd->dev,
  2815. "%s: timeout for read retry\n", __func__);
  2816. return -EAGAIN;
  2817. }
  2818. if (atomic_read(&prtd->read_abort)) {
  2819. dev_err(rtd->dev,
  2820. "%s: Read abort received\n", __func__);
  2821. return -EIO;
  2822. }
  2823. prtd->appl_cnt = prtd->appl_cnt %
  2824. prtd->lsm_client->out_hw_params.period_count;
  2825. pcm_buf = prtd->lsm_client->lab_buffer[prtd->appl_cnt].data;
  2826. dev_dbg(rtd->dev,
  2827. "%s: copy the pcm data size %lu\n",
  2828. __func__, fbytes);
  2829. if (pcm_buf) {
  2830. if (copy_to_user(buf, pcm_buf, fbytes)) {
  2831. dev_err(rtd->dev,
  2832. "%s: failed to copy bytes %lu\n",
  2833. __func__, fbytes);
  2834. return -EINVAL;
  2835. }
  2836. } else {
  2837. dev_err(rtd->dev,
  2838. "%s: Invalid pcm buffer\n", __func__);
  2839. return -EINVAL;
  2840. }
  2841. prtd->appl_cnt = (prtd->appl_cnt + 1) %
  2842. prtd->lsm_client->out_hw_params.period_count;
  2843. spin_lock_irqsave(&prtd->xrun_lock, flags);
  2844. /* Queue lab buffer here if in xrun */
  2845. if (prtd->xrun_count > 0) {
  2846. (prtd->xrun_count)--;
  2847. buf_index = (prtd->xrun_index + 1) %
  2848. prtd->lsm_client->out_hw_params.period_count;
  2849. rc = msm_lsm_queue_lab_buffer(prtd, buf_index);
  2850. if (rc)
  2851. dev_err(rtd->dev,
  2852. "%s: error in queuing the lab buffer rc %d\n",
  2853. __func__, rc);
  2854. prtd->xrun_index = buf_index;
  2855. }
  2856. atomic_dec(&prtd->buf_count);
  2857. spin_unlock_irqrestore(&prtd->xrun_lock, flags);
  2858. return 0;
  2859. }
  2860. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2861. static int msm_lsm_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  2862. struct snd_ctl_elem_value *ucontrol)
  2863. {
  2864. u64 fe_id = kcontrol->private_value;
  2865. int session_type = SESSION_TYPE_TX;
  2866. int be_id = ucontrol->value.integer.value[3];
  2867. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  2868. int ret = 0;
  2869. cfg_data.app_type = ucontrol->value.integer.value[0];
  2870. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  2871. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  2872. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  2873. __func__, fe_id, session_type, be_id,
  2874. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  2875. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  2876. be_id, &cfg_data);
  2877. if (ret < 0)
  2878. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  2879. __func__, ret);
  2880. return 0;
  2881. }
  2882. static int msm_lsm_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  2883. struct snd_ctl_elem_value *ucontrol)
  2884. {
  2885. u64 fe_id = kcontrol->private_value;
  2886. int session_type = SESSION_TYPE_TX;
  2887. int be_id = 0;
  2888. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  2889. int ret = 0;
  2890. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  2891. &be_id, &cfg_data);
  2892. if (ret < 0) {
  2893. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  2894. __func__, ret);
  2895. goto done;
  2896. }
  2897. ucontrol->value.integer.value[0] = cfg_data.app_type;
  2898. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  2899. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  2900. ucontrol->value.integer.value[3] = be_id;
  2901. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  2902. __func__, fe_id, session_type, be_id,
  2903. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  2904. done:
  2905. return ret;
  2906. }
  2907. static int msm_lsm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  2908. {
  2909. struct snd_pcm *pcm = rtd->pcm;
  2910. struct snd_pcm_usr *app_type_info;
  2911. struct snd_kcontrol *kctl;
  2912. const char *mixer_ctl_name = "Listen Stream";
  2913. const char *deviceNo = "NN";
  2914. const char *suffix = "App Type Cfg";
  2915. int ctl_len, ret = 0;
  2916. ctl_len = strlen(mixer_ctl_name) + 1 +
  2917. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  2918. pr_debug("%s: Listen app type cntrl add\n", __func__);
  2919. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  2920. NULL, 1, ctl_len, rtd->dai_link->id,
  2921. &app_type_info);
  2922. if (ret < 0) {
  2923. pr_err("%s: Listen app type cntrl add failed: %d\n",
  2924. __func__, ret);
  2925. return ret;
  2926. }
  2927. kctl = app_type_info->kctl;
  2928. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  2929. mixer_ctl_name, rtd->pcm->device, suffix);
  2930. kctl->put = msm_lsm_app_type_cfg_ctl_put;
  2931. kctl->get = msm_lsm_app_type_cfg_ctl_get;
  2932. return 0;
  2933. }
  2934. #else
  2935. static int msm_lsm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  2936. {
  2937. return 0;
  2938. }
  2939. #endif /* CONFIG_AUDIO_QGKI */
  2940. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2941. static int msm_lsm_afe_data_ctl_put(struct snd_kcontrol *kcontrol,
  2942. struct snd_ctl_elem_value *ucontrol)
  2943. {
  2944. u64 fe_id = kcontrol->private_value;
  2945. uint16_t afe_data_format = 0;
  2946. int ret = 0;
  2947. afe_data_format = ucontrol->value.integer.value[0];
  2948. pr_debug("%s: afe data is %s\n", __func__,
  2949. afe_data_format ? "unprocessed" : "processed");
  2950. ret = q6lsm_set_afe_data_format(fe_id, afe_data_format);
  2951. if (ret)
  2952. pr_err("%s: q6lsm_set_afe_data_format failed, ret = %d\n",
  2953. __func__, ret);
  2954. return ret;
  2955. }
  2956. static int msm_lsm_afe_data_ctl_get(struct snd_kcontrol *kcontrol,
  2957. struct snd_ctl_elem_value *ucontrol)
  2958. {
  2959. u64 fe_id = kcontrol->private_value;
  2960. uint16_t afe_data_format = 0;
  2961. int ret = 0;
  2962. q6lsm_get_afe_data_format(fe_id, &afe_data_format);
  2963. ucontrol->value.integer.value[0] = afe_data_format;
  2964. pr_debug("%s: afe data is %s\n", __func__,
  2965. afe_data_format ? "unprocessed" : "processed");
  2966. return ret;
  2967. }
  2968. static int msm_lsm_add_afe_data_controls(struct snd_soc_pcm_runtime *rtd)
  2969. {
  2970. struct snd_pcm *pcm = rtd->pcm;
  2971. struct snd_pcm_usr *afe_data_info;
  2972. struct snd_kcontrol *kctl;
  2973. const char *mixer_ctl_name = "Listen Stream";
  2974. const char *deviceNo = "NN";
  2975. const char *suffix = "Unprocessed Data";
  2976. int ctl_len, ret = 0;
  2977. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
  2978. strlen(suffix) + 1;
  2979. pr_debug("%s: Adding Listen afe data cntrls\n", __func__);
  2980. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  2981. NULL, 1, ctl_len, rtd->dai_link->id,
  2982. &afe_data_info);
  2983. if (ret < 0) {
  2984. pr_err("%s: Adding Listen afe data cntrls failed: %d\n",
  2985. __func__, ret);
  2986. return ret;
  2987. }
  2988. kctl = afe_data_info->kctl;
  2989. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  2990. mixer_ctl_name, rtd->pcm->device, suffix);
  2991. kctl->put = msm_lsm_afe_data_ctl_put;
  2992. kctl->get = msm_lsm_afe_data_ctl_get;
  2993. return 0;
  2994. }
  2995. #else
  2996. static int msm_lsm_add_afe_data_controls(struct snd_soc_pcm_runtime *rtd)
  2997. {
  2998. return 0;
  2999. }
  3000. #endif /* CONFIG_AUDIO_QGKI */
  3001. static int msm_lsm_add_controls(struct snd_soc_pcm_runtime *rtd)
  3002. {
  3003. int ret = 0;
  3004. ret = msm_lsm_add_app_type_controls(rtd);
  3005. if (ret)
  3006. pr_err("%s, add app type controls failed:%d\n", __func__, ret);
  3007. ret = msm_lsm_add_afe_data_controls(rtd);
  3008. if (ret)
  3009. pr_err("%s, add afe data controls failed:%d\n", __func__, ret);
  3010. return ret;
  3011. }
  3012. static const struct snd_pcm_ops msm_lsm_ops = {
  3013. .open = msm_lsm_open,
  3014. .close = msm_lsm_close,
  3015. .ioctl = msm_lsm_ioctl,
  3016. .prepare = msm_lsm_prepare,
  3017. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  3018. .compat_ioctl = msm_lsm_ioctl_compat,
  3019. #endif /* CONFIG_AUDIO_QGKI */
  3020. .hw_params = msm_lsm_hw_params,
  3021. .copy_user = msm_lsm_pcm_copy,
  3022. .pointer = msm_lsm_pcm_pointer,
  3023. };
  3024. static int msm_asoc_lsm_new(struct snd_soc_pcm_runtime *rtd)
  3025. {
  3026. struct snd_card *card = rtd->card->snd_card;
  3027. int ret = 0;
  3028. if (!card->dev->coherent_dma_mask)
  3029. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  3030. ret = msm_lsm_add_controls(rtd);
  3031. if (ret)
  3032. pr_err("%s, kctl add failed:%d\n", __func__, ret);
  3033. return ret;
  3034. }
  3035. static int msm_asoc_lsm_probe(struct snd_soc_component *component)
  3036. {
  3037. pr_debug("enter %s\n", __func__);
  3038. return 0;
  3039. }
  3040. static struct snd_soc_component_driver msm_soc_component = {
  3041. .name = DRV_NAME,
  3042. .ops = &msm_lsm_ops,
  3043. .pcm_new = msm_asoc_lsm_new,
  3044. .probe = msm_asoc_lsm_probe,
  3045. };
  3046. static int msm_lsm_probe(struct platform_device *pdev)
  3047. {
  3048. return snd_soc_register_component(&pdev->dev, &msm_soc_component,
  3049. NULL, 0);
  3050. }
  3051. static int msm_lsm_remove(struct platform_device *pdev)
  3052. {
  3053. snd_soc_unregister_component(&pdev->dev);
  3054. return 0;
  3055. }
  3056. static const struct of_device_id msm_lsm_client_dt_match[] = {
  3057. {.compatible = "qcom,msm-lsm-client" },
  3058. { }
  3059. };
  3060. static struct platform_driver msm_lsm_driver = {
  3061. .driver = {
  3062. .name = "msm-lsm-client",
  3063. .owner = THIS_MODULE,
  3064. .of_match_table = of_match_ptr(msm_lsm_client_dt_match),
  3065. .suppress_bind_attrs = true,
  3066. },
  3067. .probe = msm_lsm_probe,
  3068. .remove = msm_lsm_remove,
  3069. };
  3070. int __init msm_lsm_client_init(void)
  3071. {
  3072. return platform_driver_register(&msm_lsm_driver);
  3073. }
  3074. void msm_lsm_client_exit(void)
  3075. {
  3076. platform_driver_unregister(&msm_lsm_driver);
  3077. }
  3078. MODULE_DESCRIPTION("LSM client platform driver");
  3079. MODULE_DEVICE_TABLE(of, msm_lsm_client_dt_match);
  3080. MODULE_LICENSE("GPL v2");