msm-lsm-client.c 87 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227
  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_confidence_levels == 0) {
  465. pr_debug("%s: no confidence levels provided\n",
  466. __func__);
  467. client->confidence_levels = NULL;
  468. goto done;
  469. }
  470. client->confidence_levels =
  471. kzalloc((sizeof(uint8_t) * client->num_confidence_levels),
  472. GFP_KERNEL);
  473. if (!client->confidence_levels) {
  474. pr_err("%s: No memory for confidence\n"
  475. "levels num of level from user = %d\n",
  476. __func__, client->num_confidence_levels);
  477. rc = -ENOMEM;
  478. goto done;
  479. }
  480. if (copy_from_user(client->confidence_levels,
  481. conf_levels_ptr,
  482. client->num_confidence_levels)) {
  483. pr_err("%s: copy from user failed, size = %d\n",
  484. __func__, client->num_confidence_levels);
  485. rc = -EFAULT;
  486. goto copy_err;
  487. }
  488. return rc;
  489. copy_err:
  490. kfree(client->confidence_levels);
  491. client->confidence_levels = NULL;
  492. done:
  493. return rc;
  494. }
  495. static int msm_lsm_set_epd(struct snd_pcm_substream *substream,
  496. struct lsm_params_info_v2 *p_info)
  497. {
  498. struct snd_pcm_runtime *runtime = substream->runtime;
  499. struct lsm_priv *prtd = runtime->private_data;
  500. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  501. int rc = 0;
  502. struct snd_lsm_ep_det_thres epd_th;
  503. if (p_info->param_size != sizeof(epd_th)) {
  504. dev_err(rtd->dev,
  505. "%s: Invalid param_size %d\n",
  506. __func__, p_info->param_size);
  507. rc = -EINVAL;
  508. goto done;
  509. }
  510. if (copy_from_user(&epd_th, p_info->param_data,
  511. p_info->param_size)) {
  512. dev_err(rtd->dev,
  513. "%s: copy_from_user failed, size = %d\n",
  514. __func__, p_info->param_size);
  515. rc = -EFAULT;
  516. goto done;
  517. }
  518. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  519. &epd_th, LSM_ENDPOINT_DETECT_THRESHOLD);
  520. if (rc)
  521. dev_err(rtd->dev,
  522. "%s: Failed to set epd param, err = %d\n",
  523. __func__, rc);
  524. done:
  525. return rc;
  526. }
  527. static int msm_lsm_set_mode(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. struct snd_lsm_detect_mode mode;
  534. int rc = 0;
  535. if (p_info->param_size != sizeof(mode)) {
  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(&mode, p_info->param_data,
  543. sizeof(mode))) {
  544. dev_err(rtd->dev,
  545. "%s: copy_from_user failed, size = %zd\n",
  546. __func__, sizeof(mode));
  547. rc = -EFAULT;
  548. goto done;
  549. }
  550. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  551. &mode, LSM_OPERATION_MODE);
  552. if (rc)
  553. dev_err(rtd->dev,
  554. "%s: Failed to set det_mode param, err = %d\n",
  555. __func__, rc);
  556. done:
  557. return rc;
  558. }
  559. static int msm_lsm_set_gain(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_gain gain;
  566. int rc = 0;
  567. if (p_info->param_size != sizeof(gain)) {
  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(&gain, p_info->param_data,
  575. sizeof(gain))) {
  576. dev_err(rtd->dev,
  577. "%s: copy_from_user failed, size = %zd\n",
  578. __func__, sizeof(gain));
  579. rc = -EFAULT;
  580. goto done;
  581. }
  582. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  583. &gain, LSM_GAIN);
  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_conf(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. int rc = 0;
  598. if (p_info->param_size > MAX_NUM_CONFIDENCE) {
  599. dev_err(rtd->dev,
  600. "%s: invalid confidence levels %d\n",
  601. __func__, p_info->param_size);
  602. return -EINVAL;
  603. }
  604. prtd->lsm_client->num_confidence_levels =
  605. p_info->param_size;
  606. rc = msm_lsm_get_conf_levels(prtd->lsm_client,
  607. p_info->param_data);
  608. if (rc) {
  609. dev_err(rtd->dev,
  610. "%s: get_conf_levels failed, err = %d\n",
  611. __func__, rc);
  612. return rc;
  613. }
  614. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  615. prtd->lsm_client->confidence_levels,
  616. LSM_MIN_CONFIDENCE_LEVELS);
  617. if (rc)
  618. dev_err(rtd->dev,
  619. "%s: Failed to set min_conf_levels, err = %d\n",
  620. __func__, rc);
  621. if (prtd->lsm_client->confidence_levels) {
  622. kfree(prtd->lsm_client->confidence_levels);
  623. prtd->lsm_client->confidence_levels = NULL;
  624. }
  625. return rc;
  626. }
  627. static int msm_lsm_reg_model(struct snd_pcm_substream *substream,
  628. struct lsm_params_info_v2 *p_info)
  629. {
  630. struct snd_pcm_runtime *runtime = substream->runtime;
  631. struct lsm_priv *prtd = runtime->private_data;
  632. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  633. int rc = 0;
  634. struct lsm_sound_model *sm = NULL;
  635. size_t offset = sizeof(union param_hdrs);
  636. rc = q6lsm_snd_model_buf_alloc(prtd->lsm_client,
  637. p_info->param_size, p_info);
  638. if (rc) {
  639. dev_err(rtd->dev,
  640. "%s: snd_model buf alloc failed, size = %d\n",
  641. __func__, p_info->param_size);
  642. return rc;
  643. }
  644. q6lsm_sm_set_param_data(prtd->lsm_client, p_info, &offset);
  645. /*
  646. * For set_param, advance the sound model data with the
  647. * number of bytes required by param_data.
  648. */
  649. sm = &prtd->lsm_client->stage_cfg[p_info->stage_idx].sound_model;
  650. if (copy_from_user((u8 *)sm->data + offset,
  651. p_info->param_data, p_info->param_size)) {
  652. dev_err(rtd->dev,
  653. "%s: copy_from_user for snd_model failed, size = %d\n",
  654. __func__, p_info->param_size);
  655. rc = -EFAULT;
  656. goto err_copy;
  657. }
  658. rc = q6lsm_set_one_param(prtd->lsm_client, p_info, NULL,
  659. LSM_REG_SND_MODEL);
  660. if (rc) {
  661. dev_err(rtd->dev,
  662. "%s: Failed to set sound_model, err = %d\n",
  663. __func__, rc);
  664. goto err_copy;
  665. }
  666. return rc;
  667. err_copy:
  668. q6lsm_snd_model_buf_free(prtd->lsm_client, p_info);
  669. return rc;
  670. }
  671. static int msm_lsm_dereg_model(struct snd_pcm_substream *substream,
  672. struct lsm_params_info_v2 *p_info)
  673. {
  674. struct snd_pcm_runtime *runtime = substream->runtime;
  675. struct lsm_priv *prtd = runtime->private_data;
  676. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  677. int rc = 0;
  678. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  679. NULL, LSM_DEREG_SND_MODEL);
  680. if (rc)
  681. dev_err(rtd->dev,
  682. "%s: Failed to set det_mode param, err = %d\n",
  683. __func__, rc);
  684. q6lsm_snd_model_buf_free(prtd->lsm_client, p_info);
  685. return rc;
  686. }
  687. static int msm_lsm_set_custom(struct snd_pcm_substream *substream,
  688. struct lsm_params_info_v2 *p_info)
  689. {
  690. struct snd_pcm_runtime *runtime = substream->runtime;
  691. struct lsm_priv *prtd = runtime->private_data;
  692. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  693. u8 *data;
  694. int rc = 0;
  695. data = kzalloc(p_info->param_size, GFP_KERNEL);
  696. if (!data)
  697. return -ENOMEM;
  698. if (copy_from_user(data, p_info->param_data,
  699. p_info->param_size)) {
  700. dev_err(rtd->dev,
  701. "%s: copy_from_user failed for custom params, size = %d\n",
  702. __func__, p_info->param_size);
  703. rc = -EFAULT;
  704. goto err_ret;
  705. }
  706. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  707. data, LSM_CUSTOM_PARAMS);
  708. if (rc)
  709. dev_err(rtd->dev,
  710. "%s: Failed to set custom param, err = %d\n",
  711. __func__, rc);
  712. err_ret:
  713. kfree(data);
  714. return rc;
  715. }
  716. static int msm_lsm_check_and_set_lab_controls(struct snd_pcm_substream *substream,
  717. u32 enable, struct lsm_params_info_v2 *p_info)
  718. {
  719. struct snd_pcm_runtime *runtime = substream->runtime;
  720. struct lsm_priv *prtd = runtime->private_data;
  721. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  722. struct lsm_hw_params *out_hw_params = &prtd->lsm_client->out_hw_params;
  723. u8 *chmap = NULL;
  724. u32 ch_idx;
  725. int rc = 0, stage_idx = p_info->stage_idx;
  726. if (prtd->lsm_client->stage_cfg[stage_idx].lab_enable == enable) {
  727. dev_dbg(rtd->dev, "%s: Lab for session %d, stage %d already %s\n",
  728. __func__, prtd->lsm_client->session,
  729. stage_idx, enable ? "enabled" : "disabled");
  730. return rc;
  731. }
  732. chmap = kzalloc(out_hw_params->num_chs, GFP_KERNEL);
  733. if (!chmap)
  734. return -ENOMEM;
  735. rc = q6lsm_lab_control(prtd->lsm_client, enable, p_info);
  736. if (rc) {
  737. dev_err(rtd->dev, "%s: Failed to set lab_control param, err = %d\n",
  738. __func__, rc);
  739. goto fail;
  740. } else {
  741. if (LSM_IS_LAST_STAGE(prtd->lsm_client, stage_idx)) {
  742. rc = msm_lsm_lab_buffer_alloc(prtd,
  743. enable ? LAB_BUFFER_ALLOC : LAB_BUFFER_DEALLOC);
  744. if (rc) {
  745. dev_err(rtd->dev,
  746. "%s: msm_lsm_lab_buffer_alloc failed rc %d for %s\n",
  747. __func__, rc, enable ? "ALLOC" : "DEALLOC");
  748. goto fail;
  749. } else {
  750. /* set client level flag based on last stage control */
  751. prtd->lsm_client->lab_enable = enable;
  752. }
  753. }
  754. if (!rc)
  755. prtd->lsm_client->stage_cfg[stage_idx].lab_enable = enable;
  756. }
  757. /*
  758. * First channel to be read from lab is always the
  759. * best channel (0xff). For second channel onwards,
  760. * the channel indices are 0, 1, .. etc
  761. */
  762. chmap[0] = 0xFF;
  763. for (ch_idx = 1; ch_idx < out_hw_params->num_chs; ch_idx++)
  764. chmap[ch_idx] = ch_idx - 1;
  765. rc = q6lsm_lab_out_ch_cfg(prtd->lsm_client, chmap, p_info);
  766. if (rc)
  767. dev_err(rtd->dev, "%s: Failed to set lab out ch cfg %d\n",
  768. __func__, rc);
  769. fail:
  770. kfree(chmap);
  771. return rc;
  772. }
  773. static int msm_lsm_set_lab_control(struct snd_pcm_substream *substream,
  774. struct lsm_params_info_v2 *p_info)
  775. {
  776. struct snd_pcm_runtime *runtime = substream->runtime;
  777. struct lsm_priv *prtd = runtime->private_data;
  778. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  779. struct snd_lsm_lab_control lab_ctrl;
  780. int rc = 0;
  781. if (p_info->param_size != sizeof(lab_ctrl))
  782. return -EINVAL;
  783. if (prtd->lsm_client->started) {
  784. dev_err(rtd->dev, "%s: lab control sent after start\n", __func__);
  785. return -EAGAIN;
  786. }
  787. if (copy_from_user(&lab_ctrl, p_info->param_data,
  788. p_info->param_size)) {
  789. dev_err(rtd->dev,
  790. "%s: copy_from_user failed for lab_control params, size = %d\n",
  791. __func__, p_info->param_size);
  792. return -EFAULT;
  793. }
  794. rc = msm_lsm_check_and_set_lab_controls(substream, lab_ctrl.enable, p_info);
  795. return rc;
  796. }
  797. static int msm_lsm_set_poll_enable(struct snd_pcm_substream *substream,
  798. struct lsm_params_info_v2 *p_info)
  799. {
  800. struct snd_pcm_runtime *runtime = substream->runtime;
  801. struct lsm_priv *prtd = runtime->private_data;
  802. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  803. struct snd_lsm_poll_enable poll_enable;
  804. int rc = 0;
  805. if (p_info->param_size != sizeof(poll_enable)) {
  806. dev_err(rtd->dev,
  807. "%s: Invalid param_size %d\n",
  808. __func__, p_info->param_size);
  809. rc = -EINVAL;
  810. goto done;
  811. }
  812. if (copy_from_user(&poll_enable, p_info->param_data,
  813. sizeof(poll_enable))) {
  814. dev_err(rtd->dev,
  815. "%s: copy_from_user failed, size = %zd\n",
  816. __func__, sizeof(poll_enable));
  817. rc = -EFAULT;
  818. goto done;
  819. }
  820. if (prtd->lsm_client->poll_enable == poll_enable.poll_en) {
  821. dev_dbg(rtd->dev,
  822. "%s: Polling for session %d already %s\n",
  823. __func__, prtd->lsm_client->session,
  824. (poll_enable.poll_en ? "enabled" : "disabled"));
  825. rc = 0;
  826. goto done;
  827. }
  828. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  829. &poll_enable, LSM_POLLING_ENABLE);
  830. if (!rc) {
  831. prtd->lsm_client->poll_enable = poll_enable.poll_en;
  832. } else {
  833. dev_err(rtd->dev,
  834. "%s: Failed to set poll enable, err = %d\n",
  835. __func__, rc);
  836. }
  837. done:
  838. return rc;
  839. }
  840. static int msm_lsm_set_det_event_type(struct snd_pcm_substream *substream,
  841. struct lsm_params_info_v2 *p_info)
  842. {
  843. struct snd_lsm_det_event_type det_event_type;
  844. struct snd_pcm_runtime *runtime = substream->runtime;
  845. struct lsm_priv *prtd = runtime->private_data;
  846. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  847. int rc = 0;
  848. if (p_info->param_size != sizeof(det_event_type)) {
  849. dev_err(rtd->dev,
  850. "%s: Invalid param_size %d\n",
  851. __func__, p_info->param_size);
  852. rc = -EINVAL;
  853. goto done;
  854. }
  855. if (copy_from_user(&det_event_type, p_info->param_data,
  856. sizeof(det_event_type))) {
  857. dev_err(rtd->dev,
  858. "%s: copy_from_user failed, size = %zd\n",
  859. __func__, sizeof(det_event_type));
  860. rc = -EFAULT;
  861. goto done;
  862. }
  863. rc = q6lsm_set_one_param(prtd->lsm_client, p_info,
  864. &det_event_type, LSM_DET_EVENT_TYPE);
  865. if (!rc)
  866. prtd->lsm_client->event_type = det_event_type.event_type;
  867. else
  868. dev_err(rtd->dev,
  869. "%s: Failed to set detection event type %s, err = %d\n",
  870. __func__, (det_event_type.event_type ?
  871. "LSM_DET_EVENT_TYPE_GENERIC" :
  872. "LSM_DET_EVENT_TYPE_LEGACY"), rc);
  873. done:
  874. return rc;
  875. }
  876. static int msm_lsm_process_params(struct snd_pcm_substream *substream,
  877. struct lsm_params_info_v2 *p_info)
  878. {
  879. struct snd_pcm_runtime *runtime = substream->runtime;
  880. struct lsm_priv *prtd = runtime->private_data;
  881. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  882. int rc = 0;
  883. dev_dbg(rtd->dev,
  884. "%s: mid=0x%x, pid=0x%x, iid=0x%x, stage_idx=%d, size=0x%x, type=%d\n",
  885. __func__, p_info->module_id, p_info->param_id, p_info->instance_id,
  886. p_info->stage_idx, p_info->param_size, p_info->param_type);
  887. if (!prtd->lsm_client ||
  888. prtd->lsm_client->num_stages <= p_info->stage_idx) {
  889. dev_err(rtd->dev,
  890. "%s: invalid stage_idx(%d) for client(%p) having num_stages(%d)\n",
  891. __func__, p_info->stage_idx, prtd->lsm_client,
  892. prtd->lsm_client ? prtd->lsm_client->num_stages : 0);
  893. return -EINVAL;
  894. }
  895. switch (p_info->param_type) {
  896. case LSM_ENDPOINT_DETECT_THRESHOLD:
  897. rc = msm_lsm_set_epd(substream, p_info);
  898. break;
  899. case LSM_OPERATION_MODE:
  900. rc = msm_lsm_set_mode(substream, p_info);
  901. break;
  902. case LSM_GAIN:
  903. rc = msm_lsm_set_gain(substream, p_info);
  904. break;
  905. case LSM_MIN_CONFIDENCE_LEVELS:
  906. rc = msm_lsm_set_conf(substream, p_info);
  907. break;
  908. case LSM_REG_SND_MODEL:
  909. rc = msm_lsm_reg_model(substream, p_info);
  910. break;
  911. case LSM_DEREG_SND_MODEL:
  912. rc = msm_lsm_dereg_model(substream, p_info);
  913. break;
  914. case LSM_CUSTOM_PARAMS:
  915. rc = msm_lsm_set_custom(substream, p_info);
  916. break;
  917. case LSM_POLLING_ENABLE:
  918. rc = msm_lsm_set_poll_enable(substream, p_info);
  919. break;
  920. case LSM_DET_EVENT_TYPE:
  921. rc = msm_lsm_set_det_event_type(substream, p_info);
  922. break;
  923. case LSM_LAB_CONTROL:
  924. rc = msm_lsm_set_lab_control(substream, p_info);
  925. break;
  926. default:
  927. dev_err(rtd->dev,
  928. "%s: Invalid param_type %d\n",
  929. __func__, p_info->param_type);
  930. rc = -EINVAL;
  931. break;
  932. }
  933. if (rc) {
  934. pr_err("%s: set_param fail for param_type %d\n",
  935. __func__, p_info->param_type);
  936. }
  937. return rc;
  938. }
  939. static int msm_lsm_start_lab_buffer(struct lsm_priv *prtd, uint16_t status)
  940. {
  941. struct lsm_client *lsm_client = prtd->lsm_client;
  942. int rc = 0;
  943. if (lsm_client && lsm_client->lab_enable &&
  944. !lsm_client->lab_started &&
  945. status == LSM_VOICE_WAKEUP_STATUS_DETECTED) {
  946. atomic_set(&prtd->read_abort, 0);
  947. atomic_set(&prtd->buf_count, 0);
  948. prtd->appl_cnt = 0;
  949. prtd->dma_write = 0;
  950. prtd->xrun_count = 0;
  951. prtd->xrun_index = 0;
  952. rc = msm_lsm_queue_lab_buffer(prtd, 0);
  953. if (rc)
  954. pr_err("%s: Queue buffer failed for lab rc = %d\n",
  955. __func__, rc);
  956. else
  957. prtd->lsm_client->lab_started = true;
  958. }
  959. return rc;
  960. }
  961. static int msm_lsm_ioctl_shared(struct snd_pcm_substream *substream,
  962. unsigned int cmd, void *arg)
  963. {
  964. struct snd_soc_pcm_runtime *rtd;
  965. unsigned long flags;
  966. int ret;
  967. struct snd_lsm_sound_model_v2 snd_model_v2;
  968. struct snd_lsm_session_data session_data;
  969. struct snd_lsm_session_data_v2 ses_data_v2 = {0};
  970. int rc = 0, stage_idx;
  971. int xchg = 0;
  972. struct snd_pcm_runtime *runtime;
  973. struct lsm_priv *prtd;
  974. struct snd_lsm_detection_params det_params;
  975. uint32_t max_detection_stages_supported = LSM_MAX_STAGES_PER_SESSION;
  976. if (!substream || !substream->private_data) {
  977. pr_err("%s: Invalid %s\n", __func__,
  978. (!substream) ? "substream" : "private_data");
  979. return -EINVAL;
  980. }
  981. runtime = substream->runtime;
  982. prtd = runtime->private_data;
  983. rtd = substream->private_data;
  984. dev_dbg(rtd->dev, "%s: enter, cmd %x\n", __func__, cmd);
  985. switch (cmd) {
  986. case SNDRV_LSM_SET_SESSION_DATA:
  987. case SNDRV_LSM_SET_SESSION_DATA_V2:
  988. if (cmd == SNDRV_LSM_SET_SESSION_DATA) {
  989. dev_dbg(rtd->dev, "%s: set session data\n", __func__);
  990. rc = copy_from_user(&session_data, arg, sizeof(session_data));
  991. if (!rc) {
  992. ses_data_v2.app_id = session_data.app_id;
  993. ses_data_v2.num_stages = 1;
  994. }
  995. } else {
  996. dev_dbg(rtd->dev, "%s: set session data_v2\n", __func__);
  997. rc = copy_from_user(&ses_data_v2, arg, sizeof(ses_data_v2));
  998. }
  999. if (rc) {
  1000. dev_err(rtd->dev, "%s: %s: copy_from_user failed\n",
  1001. __func__, "LSM_SET_SESSION_DATA(_V2)");
  1002. return -EFAULT;
  1003. }
  1004. if (ses_data_v2.app_id != LSM_VOICE_WAKEUP_APP_ID_V2) {
  1005. dev_err(rtd->dev,
  1006. "%s:Invalid App id %d for Listen client\n",
  1007. __func__, ses_data_v2.app_id);
  1008. rc = -EINVAL;
  1009. break;
  1010. }
  1011. /*
  1012. * Before validating num_stages from user argument.
  1013. * Check ADSP support for multi-stage session,
  1014. * and reset max_detection_stages_supported to "1" if required.
  1015. */
  1016. if (!q6lsm_adsp_supports_multi_stage_detection()) {
  1017. dev_dbg(rtd->dev,
  1018. "%s: multi-stage session not supported by adsp\n", __func__);
  1019. max_detection_stages_supported = 1;
  1020. }
  1021. if (ses_data_v2.num_stages <= 0 ||
  1022. ses_data_v2.num_stages > max_detection_stages_supported) {
  1023. dev_err(rtd->dev,
  1024. "%s: Unsupported number of stages req(%d)/max(%d)\n",
  1025. __func__, ses_data_v2.num_stages,
  1026. max_detection_stages_supported);
  1027. rc = -EINVAL;
  1028. break;
  1029. }
  1030. prtd->lsm_client->app_id = ses_data_v2.app_id;
  1031. prtd->lsm_client->num_stages = ses_data_v2.num_stages;
  1032. for (stage_idx = LSM_STAGE_INDEX_FIRST;
  1033. stage_idx < ses_data_v2.num_stages; stage_idx++) {
  1034. prtd->lsm_client->stage_cfg[stage_idx].app_type =
  1035. ses_data_v2.stage_info[stage_idx].app_type;
  1036. prtd->lsm_client->stage_cfg[stage_idx].lpi_enable =
  1037. ses_data_v2.stage_info[stage_idx].lpi_enable;
  1038. }
  1039. ret = q6lsm_open(prtd->lsm_client, ses_data_v2.app_id);
  1040. if (ret < 0) {
  1041. dev_err(rtd->dev,
  1042. "%s: lsm open failed, %d\n",
  1043. __func__, ret);
  1044. __pm_relax(prtd->ws);
  1045. return ret;
  1046. }
  1047. prtd->lsm_client->opened = true;
  1048. dev_dbg(rtd->dev, "%s: Session_ID = %d, APP ID = %d, Num stages %d\n",
  1049. __func__,
  1050. prtd->lsm_client->session,
  1051. prtd->lsm_client->app_id,
  1052. prtd->lsm_client->num_stages);
  1053. break;
  1054. case SNDRV_LSM_REG_SND_MODEL_V2: {
  1055. /*
  1056. * With multi-stage support sm buff allocation/free usage param info
  1057. * to check stage index for which this sound model is being set, and
  1058. * to check whether sm data is sent using set param command or not.
  1059. * Hence, set param ids to '0' to indicate allocation is for legacy
  1060. * reg_sm cmd, where buffer for param header need not be allocated,
  1061. * also set stage index to LSM_STAGE_INDEX_FIRST.
  1062. */
  1063. struct lsm_params_info_v2 p_info = {0};
  1064. p_info.stage_idx = LSM_STAGE_INDEX_FIRST;
  1065. dev_dbg(rtd->dev, "%s: Registering sound model V2\n",
  1066. __func__);
  1067. memcpy(&snd_model_v2, arg,
  1068. sizeof(struct snd_lsm_sound_model_v2));
  1069. if (snd_model_v2.num_confidence_levels >
  1070. MAX_NUM_CONFIDENCE) {
  1071. dev_err(rtd->dev,
  1072. "%s: Invalid conf_levels = %d, maximum allowed = %d\n",
  1073. __func__, snd_model_v2.num_confidence_levels,
  1074. MAX_NUM_CONFIDENCE);
  1075. rc = -EINVAL;
  1076. break;
  1077. }
  1078. rc = q6lsm_snd_model_buf_alloc(prtd->lsm_client,
  1079. snd_model_v2.data_size, &p_info);
  1080. if (rc) {
  1081. dev_err(rtd->dev,
  1082. "%s: q6lsm buffer alloc failed V2, size %d\n",
  1083. __func__, snd_model_v2.data_size);
  1084. break;
  1085. }
  1086. if (copy_from_user(
  1087. prtd->lsm_client->stage_cfg[p_info.stage_idx].sound_model.data,
  1088. snd_model_v2.data, snd_model_v2.data_size)) {
  1089. dev_err(rtd->dev,
  1090. "%s: copy from user data failed\n"
  1091. "data %pK size %d\n", __func__,
  1092. snd_model_v2.data, snd_model_v2.data_size);
  1093. q6lsm_snd_model_buf_free(prtd->lsm_client, &p_info);
  1094. rc = -EFAULT;
  1095. break;
  1096. }
  1097. dev_dbg(rtd->dev, "SND Model Magic no byte[0] %x,\n"
  1098. "byte[1] %x, byte[2] %x byte[3] %x\n",
  1099. snd_model_v2.data[0], snd_model_v2.data[1],
  1100. snd_model_v2.data[2], snd_model_v2.data[3]);
  1101. prtd->lsm_client->num_confidence_levels =
  1102. snd_model_v2.num_confidence_levels;
  1103. rc = msm_lsm_get_conf_levels(prtd->lsm_client,
  1104. snd_model_v2.confidence_level);
  1105. if (rc) {
  1106. dev_err(rtd->dev,
  1107. "%s: get_conf_levels failed, err = %d\n",
  1108. __func__, rc);
  1109. break;
  1110. }
  1111. rc = q6lsm_register_sound_model(prtd->lsm_client,
  1112. snd_model_v2.detection_mode,
  1113. snd_model_v2.detect_failure);
  1114. if (rc < 0) {
  1115. dev_err(rtd->dev,
  1116. "%s: Register snd Model v2 failed =%d\n",
  1117. __func__, rc);
  1118. q6lsm_snd_model_buf_free(prtd->lsm_client, &p_info);
  1119. }
  1120. if (prtd->lsm_client->confidence_levels) {
  1121. kfree(prtd->lsm_client->confidence_levels);
  1122. prtd->lsm_client->confidence_levels = NULL;
  1123. }
  1124. break;
  1125. }
  1126. case SNDRV_LSM_SET_PARAMS:
  1127. dev_dbg(rtd->dev, "%s: set_params\n", __func__);
  1128. memcpy(&det_params, arg,
  1129. sizeof(det_params));
  1130. if (det_params.num_confidence_levels >
  1131. MAX_NUM_CONFIDENCE) {
  1132. rc = -EINVAL;
  1133. break;
  1134. }
  1135. prtd->lsm_client->num_confidence_levels =
  1136. det_params.num_confidence_levels;
  1137. rc = msm_lsm_get_conf_levels(prtd->lsm_client,
  1138. det_params.conf_level);
  1139. if (rc) {
  1140. dev_err(rtd->dev,
  1141. "%s: Failed to get conf_levels, err = %d\n",
  1142. __func__, rc);
  1143. break;
  1144. }
  1145. rc = q6lsm_set_data(prtd->lsm_client,
  1146. det_params.detect_mode,
  1147. det_params.detect_failure);
  1148. if (rc)
  1149. dev_err(rtd->dev,
  1150. "%s: Failed to set params, err = %d\n",
  1151. __func__, rc);
  1152. if (prtd->lsm_client->confidence_levels) {
  1153. kfree(prtd->lsm_client->confidence_levels);
  1154. prtd->lsm_client->confidence_levels = NULL;
  1155. }
  1156. break;
  1157. case SNDRV_LSM_DEREG_SND_MODEL:
  1158. dev_dbg(rtd->dev, "%s: Deregistering sound model\n",
  1159. __func__);
  1160. rc = q6lsm_deregister_sound_model(prtd->lsm_client);
  1161. if (rc)
  1162. dev_err(rtd->dev,
  1163. "%s: Sound model de-register failed, err = %d\n",
  1164. __func__, rc);
  1165. break;
  1166. case SNDRV_LSM_EVENT_STATUS:
  1167. case SNDRV_LSM_EVENT_STATUS_V3: {
  1168. uint32_t ts_lsw, ts_msw;
  1169. uint16_t status = 0, payload_size = 0;
  1170. dev_dbg(rtd->dev, "%s: Get event status cmd %xx\n", __func__, cmd);
  1171. atomic_set(&prtd->event_wait_stop, 0);
  1172. /*
  1173. * Release the api lock before wait to allow
  1174. * other IOCTLs to be invoked while waiting
  1175. * for event
  1176. */
  1177. mutex_unlock(&prtd->lsm_api_lock);
  1178. rc = wait_event_freezable(prtd->event_wait,
  1179. (cmpxchg(&prtd->event_avail, 1, 0) ||
  1180. (xchg = atomic_cmpxchg(&prtd->event_wait_stop,
  1181. 1, 0))));
  1182. dev_dbg(rtd->dev, "%s: wait event is done\n", __func__);
  1183. mutex_lock(&prtd->lsm_api_lock);
  1184. dev_dbg(rtd->dev, "%s: wait_event_freezable %d event_wait_stop %d\n",
  1185. __func__, rc, xchg);
  1186. if (!rc && !xchg) {
  1187. dev_dbg(rtd->dev, "%s: New event available %ld\n",
  1188. __func__, prtd->event_avail);
  1189. spin_lock_irqsave(&prtd->event_lock, flags);
  1190. if (prtd->event_status) {
  1191. payload_size = prtd->event_status->payload_size;
  1192. ts_lsw = prtd->event_status->timestamp_lsw;
  1193. ts_msw = prtd->event_status->timestamp_msw;
  1194. status = prtd->event_status->status;
  1195. spin_unlock_irqrestore(&prtd->event_lock,
  1196. flags);
  1197. } else {
  1198. spin_unlock_irqrestore(&prtd->event_lock,
  1199. flags);
  1200. rc = -EINVAL;
  1201. dev_err(rtd->dev,
  1202. "%s: prtd->event_status is NULL\n",
  1203. __func__);
  1204. break;
  1205. }
  1206. if (cmd == SNDRV_LSM_EVENT_STATUS) {
  1207. struct snd_lsm_event_status *user = arg;
  1208. if (user->payload_size < payload_size) {
  1209. dev_dbg(rtd->dev,
  1210. "%s: provided %d bytes isn't enough, needs %d bytes\n",
  1211. __func__, user->payload_size,
  1212. payload_size);
  1213. rc = -ENOMEM;
  1214. } else {
  1215. user->status = status;
  1216. user->payload_size = payload_size;
  1217. memcpy(user->payload,
  1218. prtd->event_status->payload,
  1219. payload_size);
  1220. }
  1221. } else {
  1222. struct snd_lsm_event_status_v3 *user_v3 = arg;
  1223. if (user_v3->payload_size < payload_size) {
  1224. dev_dbg(rtd->dev,
  1225. "%s: provided %d bytes isn't enough, needs %d bytes\n",
  1226. __func__, user_v3->payload_size,
  1227. payload_size);
  1228. rc = -ENOMEM;
  1229. } else {
  1230. user_v3->timestamp_lsw = ts_lsw;
  1231. user_v3->timestamp_msw = ts_msw;
  1232. user_v3->status = status;
  1233. user_v3->payload_size = payload_size;
  1234. memcpy(user_v3->payload,
  1235. prtd->event_status->payload,
  1236. payload_size);
  1237. }
  1238. }
  1239. if (!rc)
  1240. rc = msm_lsm_start_lab_buffer(prtd, status);
  1241. } else if (xchg) {
  1242. dev_dbg(rtd->dev, "%s: Wait aborted\n", __func__);
  1243. rc = 0;
  1244. }
  1245. break;
  1246. }
  1247. case SNDRV_LSM_GENERIC_DET_EVENT: {
  1248. struct snd_lsm_event_status *user = arg;
  1249. uint16_t status = 0;
  1250. uint16_t payload_size = 0;
  1251. dev_dbg(rtd->dev,
  1252. "%s: SNDRV_LSM_GENERIC_DET_EVENT\n", __func__);
  1253. atomic_set(&prtd->event_wait_stop, 0);
  1254. /*
  1255. * Release the api lock before wait to allow
  1256. * other IOCTLs to be invoked while waiting
  1257. * for event
  1258. */
  1259. mutex_unlock(&prtd->lsm_api_lock);
  1260. rc = wait_event_freezable(prtd->event_wait,
  1261. (cmpxchg(&prtd->event_avail, 1, 0) ||
  1262. (xchg = atomic_cmpxchg(&prtd->event_wait_stop,
  1263. 1, 0))));
  1264. mutex_lock(&prtd->lsm_api_lock);
  1265. dev_dbg(rtd->dev, "%s: wait_event_freezable %d event_wait_stop %d\n",
  1266. __func__, rc, xchg);
  1267. if (!rc && !xchg) {
  1268. dev_dbg(rtd->dev, "%s: %s: New event available %ld\n",
  1269. __func__, "SNDRV_LSM_GENERIC_DET_EVENT",
  1270. prtd->event_avail);
  1271. spin_lock_irqsave(&prtd->event_lock, flags);
  1272. if (prtd->det_event) {
  1273. payload_size = prtd->det_event->payload_size;
  1274. status = prtd->det_event->status;
  1275. spin_unlock_irqrestore(&prtd->event_lock,
  1276. flags);
  1277. } else {
  1278. spin_unlock_irqrestore(&prtd->event_lock,
  1279. flags);
  1280. dev_err(rtd->dev,
  1281. "%s: %s: prtd->event_status is NULL\n",
  1282. __func__,
  1283. "SNDRV_LSM_GENERIC_DET_EVENT");
  1284. rc = -EINVAL;
  1285. break;
  1286. }
  1287. if (user->payload_size < payload_size) {
  1288. dev_err(rtd->dev,
  1289. "%s: provided %d bytes isn't enough, needs %d bytes\n",
  1290. __func__, user->payload_size,
  1291. payload_size);
  1292. rc = -ENOMEM;
  1293. break;
  1294. }
  1295. user->status = status;
  1296. user->payload_size = payload_size;
  1297. memcpy(user->payload, prtd->det_event->payload,
  1298. payload_size);
  1299. rc = msm_lsm_start_lab_buffer(prtd, status);
  1300. } else if (xchg) {
  1301. dev_dbg(rtd->dev, "%s: %s: Wait aborted\n",
  1302. __func__, "SNDRV_LSM_GENERIC_DET_EVENT");
  1303. rc = 0;
  1304. }
  1305. break;
  1306. }
  1307. case SNDRV_LSM_ABORT_EVENT:
  1308. dev_dbg(rtd->dev, "%s: Aborting event status wait\n",
  1309. __func__);
  1310. atomic_set(&prtd->event_wait_stop, 1);
  1311. wake_up(&prtd->event_wait);
  1312. break;
  1313. case SNDRV_LSM_START:
  1314. dev_dbg(rtd->dev, "%s: Starting LSM client session\n",
  1315. __func__);
  1316. if (!prtd->lsm_client->started) {
  1317. rc = q6lsm_start(prtd->lsm_client, true);
  1318. if (!rc) {
  1319. prtd->lsm_client->started = true;
  1320. dev_dbg(rtd->dev, "%s: LSM client session started\n",
  1321. __func__);
  1322. }
  1323. }
  1324. break;
  1325. case SNDRV_LSM_STOP: {
  1326. dev_dbg(rtd->dev,
  1327. "%s: Stopping LSM client session\n",
  1328. __func__);
  1329. if (prtd->lsm_client->started) {
  1330. if (prtd->lsm_client->lab_enable) {
  1331. atomic_set(&prtd->read_abort, 1);
  1332. if (prtd->lsm_client->lab_started) {
  1333. rc = q6lsm_stop_lab(prtd->lsm_client);
  1334. if (rc)
  1335. dev_err(rtd->dev,
  1336. "%s: stop lab failed rc %d\n",
  1337. __func__, rc);
  1338. prtd->lsm_client->lab_started = false;
  1339. }
  1340. }
  1341. if (!atomic_read(&prtd->read_abort)) {
  1342. dev_dbg(rtd->dev,
  1343. "%s: set read_abort to stop buffering\n", __func__);
  1344. atomic_set(&prtd->read_abort, 1);
  1345. }
  1346. rc = q6lsm_stop(prtd->lsm_client, true);
  1347. if (!rc)
  1348. dev_dbg(rtd->dev,
  1349. "%s: LSM client session stopped %d\n",
  1350. __func__, rc);
  1351. prtd->lsm_client->started = false;
  1352. }
  1353. break;
  1354. }
  1355. case SNDRV_LSM_LAB_CONTROL: {
  1356. u32 enable = 0;
  1357. struct lsm_params_info_v2 p_info = {0};
  1358. if (prtd->lsm_client->num_stages > 1) {
  1359. dev_err(rtd->dev, "%s: %s: not supported for multi stage session\n",
  1360. __func__, "LSM_LAB_CONTROL");
  1361. __pm_relax(prtd->ws);
  1362. return -EINVAL;
  1363. }
  1364. if (copy_from_user(&enable, arg, sizeof(enable))) {
  1365. dev_err(rtd->dev, "%s: %s: copy_frm_user failed\n",
  1366. __func__, "LSM_LAB_CONTROL");
  1367. __pm_relax(prtd->ws);
  1368. return -EFAULT;
  1369. }
  1370. dev_dbg(rtd->dev, "%s: ioctl %s, enable = %d\n",
  1371. __func__, "SNDRV_LSM_LAB_CONTROL", enable);
  1372. if (prtd->lsm_client->started) {
  1373. dev_err(rtd->dev, "%s: ioctl %s issued after start",
  1374. __func__, "SNDRV_LSM_LAB_CONTROL");
  1375. rc = -EINVAL;
  1376. break;
  1377. }
  1378. /*
  1379. * With multi-stage support lab control needs to set param info
  1380. * specifying stage index for which this lab control is issued,
  1381. * along with values of module/instance ids applicable for the stage.
  1382. * Hence, set param info with default lab module/instance ids, and
  1383. * set stage index to LSM_STAGE_INDEX_FIRST.
  1384. */
  1385. p_info.param_type = LSM_LAB_CONTROL;
  1386. p_info.module_id = LSM_MODULE_ID_LAB;
  1387. p_info.instance_id = INSTANCE_ID_0;
  1388. p_info.stage_idx = LSM_STAGE_INDEX_FIRST;
  1389. p_info.param_size = 0;
  1390. rc = msm_lsm_check_and_set_lab_controls(substream, enable, &p_info);
  1391. break;
  1392. }
  1393. case SNDRV_LSM_STOP_LAB:
  1394. dev_dbg(rtd->dev, "%s: stopping LAB\n", __func__);
  1395. if (prtd->lsm_client->lab_enable &&
  1396. prtd->lsm_client->lab_started) {
  1397. atomic_set(&prtd->read_abort, 1);
  1398. rc = q6lsm_stop_lab(prtd->lsm_client);
  1399. if (rc)
  1400. dev_err(rtd->dev,
  1401. "%s: Lab stop failed for session %d rc %d\n",
  1402. __func__,
  1403. prtd->lsm_client->session, rc);
  1404. prtd->lsm_client->lab_started = false;
  1405. }
  1406. break;
  1407. case SNDRV_LSM_SET_PORT:
  1408. dev_dbg(rtd->dev, "%s: set LSM port\n", __func__);
  1409. rc = q6lsm_set_port_connected(prtd->lsm_client);
  1410. break;
  1411. case SNDRV_LSM_SET_FWK_MODE_CONFIG: {
  1412. u32 mode;
  1413. if (copy_from_user(&mode, arg, sizeof(mode))) {
  1414. dev_err(rtd->dev, "%s: %s: copy_frm_user failed\n",
  1415. __func__, "LSM_SET_FWK_MODE_CONFIG");
  1416. __pm_relax(prtd->ws);
  1417. return -EFAULT;
  1418. }
  1419. dev_dbg(rtd->dev, "%s: ioctl %s, enable = %d\n",
  1420. __func__, "SNDRV_LSM_SET_FWK_MODE_CONFIG", mode);
  1421. if (prtd->lsm_client->event_mode == mode) {
  1422. dev_dbg(rtd->dev,
  1423. "%s: mode for %d already set to %d\n",
  1424. __func__, prtd->lsm_client->session, mode);
  1425. rc = 0;
  1426. } else {
  1427. dev_dbg(rtd->dev, "%s: Event mode = %d\n",
  1428. __func__, mode);
  1429. rc = q6lsm_set_fwk_mode_cfg(prtd->lsm_client, mode);
  1430. if (!rc)
  1431. prtd->lsm_client->event_mode = mode;
  1432. else
  1433. dev_err(rtd->dev,
  1434. "%s: set event mode failed %d\n",
  1435. __func__, rc);
  1436. }
  1437. break;
  1438. }
  1439. case SNDRV_LSM_SET_INPUT_HW_PARAMS: {
  1440. struct lsm_hw_params *in_params;
  1441. struct snd_lsm_input_hw_params params;
  1442. if (copy_from_user(&params, arg, sizeof(params))) {
  1443. dev_err(rtd->dev, "%s: %s: copy_from_user failed\n",
  1444. __func__, "LSM_SET_INPUT_HW_PARAMS");
  1445. __pm_relax(prtd->ws);
  1446. return -EFAULT;
  1447. }
  1448. in_params = &prtd->lsm_client->in_hw_params;
  1449. in_params->sample_rate = params.sample_rate;
  1450. in_params->sample_size = params.bit_width;
  1451. in_params->num_chs = params.num_channels;
  1452. break;
  1453. }
  1454. default:
  1455. dev_dbg(rtd->dev,
  1456. "%s: Falling into default snd_lib_ioctl cmd 0x%x\n",
  1457. __func__, cmd);
  1458. rc = snd_pcm_lib_ioctl(substream, cmd, arg);
  1459. break;
  1460. }
  1461. if (!rc)
  1462. dev_dbg(rtd->dev, "%s: leave (%d)\n",
  1463. __func__, rc);
  1464. else
  1465. dev_err(rtd->dev, "%s: cmd 0x%x failed %d\n",
  1466. __func__, cmd, rc);
  1467. __pm_relax(prtd->ws);
  1468. return rc;
  1469. }
  1470. static int msm_lsm_check_event_type(struct lsm_client *lsm_client,
  1471. unsigned int cmd)
  1472. {
  1473. int err = 0;
  1474. uint32_t event_type = lsm_client->event_type;
  1475. if (cmd == SNDRV_LSM_EVENT_STATUS &&
  1476. event_type != LSM_DET_EVENT_TYPE_LEGACY) {
  1477. pr_err("%s: %s: Invalid event request\n",
  1478. __func__, "SNDRV_LSM_EVENT_STATUS");
  1479. err = -EINVAL;
  1480. } else if (cmd == SNDRV_LSM_GENERIC_DET_EVENT &&
  1481. event_type != LSM_DET_EVENT_TYPE_GENERIC) {
  1482. pr_err("%s: %s: Invalid event request\n",
  1483. __func__, "SNDRV_LSM_GENERIC_DET_EVENT");
  1484. err = -EINVAL;
  1485. }
  1486. return err;
  1487. }
  1488. #ifdef CONFIG_COMPAT
  1489. struct snd_lsm_event_status32 {
  1490. u16 status;
  1491. u16 payload_size;
  1492. u8 payload[0];
  1493. };
  1494. struct snd_lsm_event_status_v3_32 {
  1495. u32 timestamp_lsw;
  1496. u32 timestamp_msw;
  1497. u16 status;
  1498. u16 payload_size;
  1499. u8 payload[0];
  1500. };
  1501. struct snd_lsm_sound_model_v2_32 {
  1502. compat_uptr_t data;
  1503. compat_uptr_t confidence_level;
  1504. u32 data_size;
  1505. enum lsm_detection_mode detection_mode;
  1506. u8 num_confidence_levels;
  1507. bool detect_failure;
  1508. };
  1509. struct snd_lsm_detection_params_32 {
  1510. compat_uptr_t conf_level;
  1511. enum lsm_detection_mode detect_mode;
  1512. u8 num_confidence_levels;
  1513. bool detect_failure;
  1514. };
  1515. struct lsm_params_info_32 {
  1516. u32 module_id;
  1517. u32 param_id;
  1518. u32 param_size;
  1519. compat_uptr_t param_data;
  1520. uint32_t param_type;
  1521. };
  1522. struct lsm_params_info_v2_32 {
  1523. u32 module_id;
  1524. u32 param_id;
  1525. u32 param_size;
  1526. compat_uptr_t param_data;
  1527. uint32_t param_type;
  1528. u16 instance_id;
  1529. u16 stage_idx;
  1530. };
  1531. struct snd_lsm_module_params_32 {
  1532. compat_uptr_t params;
  1533. u32 num_params;
  1534. u32 data_size;
  1535. };
  1536. enum {
  1537. SNDRV_LSM_REG_SND_MODEL_V2_32 =
  1538. _IOW('U', 0x07, struct snd_lsm_sound_model_v2_32),
  1539. SNDRV_LSM_SET_PARAMS_32 =
  1540. _IOW('U', 0x0A, struct snd_lsm_detection_params_32),
  1541. SNDRV_LSM_SET_MODULE_PARAMS_32 =
  1542. _IOW('U', 0x0B, struct snd_lsm_module_params_32),
  1543. SNDRV_LSM_EVENT_STATUS_V3_32 =
  1544. _IOW('U', 0x0F, struct snd_lsm_event_status_v3_32),
  1545. SNDRV_LSM_SET_MODULE_PARAMS_V2_32 =
  1546. _IOW('U', 0x13, struct snd_lsm_module_params_32),
  1547. };
  1548. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  1549. static int msm_lsm_ioctl_compat(struct snd_pcm_substream *substream,
  1550. unsigned int cmd, void __user *arg)
  1551. {
  1552. struct snd_pcm_runtime *runtime;
  1553. struct lsm_priv *prtd;
  1554. struct snd_soc_pcm_runtime *rtd;
  1555. int err = 0;
  1556. u32 size = 0;
  1557. if (PCM_RUNTIME_CHECK(substream))
  1558. return -ENXIO;
  1559. if (!substream || !substream->private_data) {
  1560. pr_err("%s: Invalid %s\n", __func__,
  1561. (!substream) ? "substream" : "private_data");
  1562. return -EINVAL;
  1563. }
  1564. runtime = substream->runtime;
  1565. rtd = substream->private_data;
  1566. prtd = runtime->private_data;
  1567. mutex_lock(&prtd->lsm_api_lock);
  1568. switch (cmd) {
  1569. case SNDRV_LSM_EVENT_STATUS:
  1570. case SNDRV_LSM_GENERIC_DET_EVENT: {
  1571. struct snd_lsm_event_status userarg32, *user32 = NULL;
  1572. struct snd_lsm_event_status *user = NULL;
  1573. dev_dbg(rtd->dev,
  1574. "%s: %s\n", __func__,
  1575. (cmd == SNDRV_LSM_EVENT_STATUS) ?
  1576. "SNDRV_LSM_EVENT_STATUS" :
  1577. "SNDRV_LSM_GENERIC_DET_EVENT");
  1578. err = msm_lsm_check_event_type(prtd->lsm_client, cmd);
  1579. if (err)
  1580. goto done;
  1581. if (copy_from_user(&userarg32, arg, sizeof(userarg32))) {
  1582. dev_err(rtd->dev, "%s: %s: Failed to copy from user\n",
  1583. __func__, (cmd == SNDRV_LSM_EVENT_STATUS) ?
  1584. "SNDRV_LSM_EVENT_STATUS" :
  1585. "SNDRV_LSM_GENERIC_DET_EVENT");
  1586. err = -EFAULT;
  1587. goto done;
  1588. }
  1589. if (userarg32.payload_size >
  1590. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  1591. dev_err(rtd->dev,
  1592. "%s: payload_size %d is invalid, max allowed = %d\n",
  1593. __func__, userarg32.payload_size,
  1594. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  1595. err = -EINVAL;
  1596. goto done;
  1597. }
  1598. size = sizeof(*user) + userarg32.payload_size;
  1599. user = kzalloc(size, GFP_KERNEL);
  1600. if (!user) {
  1601. err = -ENOMEM;
  1602. goto done;
  1603. }
  1604. user->payload_size = userarg32.payload_size;
  1605. err = msm_lsm_ioctl_shared(substream, cmd, user);
  1606. if (err) {
  1607. dev_err(rtd->dev,
  1608. "%s: msm_lsm_ioctl_shared() failed, err = %d",
  1609. __func__, err);
  1610. kfree(user);
  1611. goto done;
  1612. }
  1613. /* Update size with actual payload size */
  1614. size = sizeof(userarg32) + user->payload_size;
  1615. if (!access_ok(arg, size)) {
  1616. dev_err(rtd->dev,
  1617. "%s: Failed to verify write, size = %d\n",
  1618. __func__, size);
  1619. err = -EFAULT;
  1620. kfree(user);
  1621. goto done;
  1622. }
  1623. user32 = kzalloc(size, GFP_KERNEL);
  1624. if (!user32) {
  1625. err = -ENOMEM;
  1626. kfree(user);
  1627. goto done;
  1628. }
  1629. user32->status = user->status;
  1630. user32->payload_size = user->payload_size;
  1631. memcpy(user32->payload, user->payload,
  1632. user32->payload_size);
  1633. if (copy_to_user(arg, user32, size)) {
  1634. dev_err(rtd->dev,
  1635. "%s: Failed to copy payload to user, size = %d",
  1636. __func__, size);
  1637. err = -EFAULT;
  1638. }
  1639. kfree(user);
  1640. kfree(user32);
  1641. break;
  1642. }
  1643. case SNDRV_LSM_EVENT_STATUS_V3_32: {
  1644. struct snd_lsm_event_status_v3_32 userarg32, *user32 = NULL;
  1645. struct snd_lsm_event_status_v3 *user = NULL;
  1646. if (prtd->lsm_client->event_type !=
  1647. LSM_DET_EVENT_TYPE_LEGACY) {
  1648. dev_err(rtd->dev,
  1649. "%s: %s: Invalid event request\n",
  1650. __func__, "SNDRV_LSM_EVENT_STATUS_V3_32");
  1651. err = -EINVAL;
  1652. goto done;
  1653. }
  1654. if (copy_from_user(&userarg32, arg, sizeof(userarg32))) {
  1655. dev_err(rtd->dev, "%s: err copyuser ioctl %s\n",
  1656. __func__, "SNDRV_LSM_EVENT_STATUS_V3_32");
  1657. err = -EFAULT;
  1658. goto done;
  1659. }
  1660. if (userarg32.payload_size >
  1661. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  1662. pr_err("%s: payload_size %d is invalid, max allowed = %d\n",
  1663. __func__, userarg32.payload_size,
  1664. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  1665. err = -EINVAL;
  1666. goto done;
  1667. }
  1668. size = sizeof(*user) + userarg32.payload_size;
  1669. user = kzalloc(size, GFP_KERNEL);
  1670. if (!user) {
  1671. dev_err(rtd->dev,
  1672. "%s: Allocation failed event status size %d\n",
  1673. __func__, size);
  1674. err = -ENOMEM;
  1675. goto done;
  1676. }
  1677. cmd = SNDRV_LSM_EVENT_STATUS_V3;
  1678. user->payload_size = userarg32.payload_size;
  1679. err = msm_lsm_ioctl_shared(substream, cmd, user);
  1680. /* Update size with actual payload size */
  1681. size = sizeof(userarg32) + user->payload_size;
  1682. if (!err && !access_ok(arg, size)) {
  1683. dev_err(rtd->dev,
  1684. "%s: write verify failed size %d\n",
  1685. __func__, size);
  1686. err = -EFAULT;
  1687. }
  1688. if (!err) {
  1689. user32 = kzalloc(size, GFP_KERNEL);
  1690. if (!user32) {
  1691. dev_err(rtd->dev,
  1692. "%s: Allocation event user status size %d\n",
  1693. __func__, size);
  1694. err = -EFAULT;
  1695. } else {
  1696. user32->timestamp_lsw = user->timestamp_lsw;
  1697. user32->timestamp_msw = user->timestamp_msw;
  1698. user32->status = user->status;
  1699. user32->payload_size = user->payload_size;
  1700. memcpy(user32->payload,
  1701. user->payload, user32->payload_size);
  1702. }
  1703. }
  1704. if (!err && (copy_to_user(arg, user32, size))) {
  1705. dev_err(rtd->dev, "%s: failed to copy payload %d",
  1706. __func__, size);
  1707. err = -EFAULT;
  1708. }
  1709. kfree(user);
  1710. kfree(user32);
  1711. if (err)
  1712. dev_err(rtd->dev, "%s: lsmevent failed %d",
  1713. __func__, err);
  1714. break;
  1715. }
  1716. case SNDRV_LSM_REG_SND_MODEL_V2_32: {
  1717. struct snd_lsm_sound_model_v2_32 snd_modelv232;
  1718. struct snd_lsm_sound_model_v2 snd_modelv2;
  1719. if (prtd->lsm_client->use_topology) {
  1720. dev_err(rtd->dev,
  1721. "%s: %s: not supported if using topology\n",
  1722. __func__, "REG_SND_MODEL_V2");
  1723. err = -EINVAL;
  1724. goto done;
  1725. }
  1726. if (copy_from_user(&snd_modelv232, arg,
  1727. sizeof(snd_modelv232))) {
  1728. err = -EFAULT;
  1729. dev_err(rtd->dev,
  1730. "%s: copy user failed, size %zd %s\n",
  1731. __func__,
  1732. sizeof(struct snd_lsm_sound_model_v2_32),
  1733. "SNDRV_LSM_REG_SND_MODEL_V2_32");
  1734. } else {
  1735. snd_modelv2.confidence_level =
  1736. compat_ptr(snd_modelv232.confidence_level);
  1737. snd_modelv2.data = compat_ptr(snd_modelv232.data);
  1738. snd_modelv2.data_size = snd_modelv232.data_size;
  1739. snd_modelv2.detect_failure =
  1740. snd_modelv232.detect_failure;
  1741. snd_modelv2.detection_mode =
  1742. snd_modelv232.detection_mode;
  1743. snd_modelv2.num_confidence_levels =
  1744. snd_modelv232.num_confidence_levels;
  1745. cmd = SNDRV_LSM_REG_SND_MODEL_V2;
  1746. err = msm_lsm_ioctl_shared(substream, cmd,
  1747. &snd_modelv2);
  1748. if (err)
  1749. dev_err(rtd->dev,
  1750. "%s: ioctl %s failed\n", __func__,
  1751. "SNDDRV_LSM_REG_SND_MODEL_V2_32");
  1752. }
  1753. break;
  1754. }
  1755. case SNDRV_LSM_SET_PARAMS_32:{
  1756. struct snd_lsm_detection_params_32 det_params32;
  1757. struct snd_lsm_detection_params det_params;
  1758. if (prtd->lsm_client->use_topology) {
  1759. dev_err(rtd->dev,
  1760. "%s: %s: not supported if using topology\n",
  1761. __func__, "SET_PARAMS_32");
  1762. err = -EINVAL;
  1763. }
  1764. if (copy_from_user(&det_params32, arg,
  1765. sizeof(det_params32))) {
  1766. err = -EFAULT;
  1767. dev_err(rtd->dev,
  1768. "%s: %s: copy_from_user failed, size = %zd\n",
  1769. __func__, "SNDRV_LSM_SET_PARAMS_32",
  1770. sizeof(det_params32));
  1771. } else {
  1772. det_params.conf_level =
  1773. compat_ptr(det_params32.conf_level);
  1774. det_params.detect_mode =
  1775. det_params32.detect_mode;
  1776. det_params.num_confidence_levels =
  1777. det_params32.num_confidence_levels;
  1778. det_params.detect_failure =
  1779. det_params32.detect_failure;
  1780. cmd = SNDRV_LSM_SET_PARAMS;
  1781. err = msm_lsm_ioctl_shared(substream, cmd,
  1782. &det_params);
  1783. if (err)
  1784. dev_err(rtd->dev,
  1785. "%s: ioctl %s failed\n", __func__,
  1786. "SNDRV_LSM_SET_PARAMS");
  1787. }
  1788. break;
  1789. }
  1790. case SNDRV_LSM_SET_MODULE_PARAMS_32:
  1791. case SNDRV_LSM_SET_MODULE_PARAMS_V2_32: {
  1792. struct snd_lsm_module_params_32 p_data_32;
  1793. struct snd_lsm_module_params p_data;
  1794. u8 *params32;
  1795. size_t expected_size = 0, count;
  1796. struct lsm_params_info_32 *p_info_32 = NULL;
  1797. struct lsm_params_info_v2_32 *p_info_v2_32 = NULL;
  1798. struct lsm_params_info_v2 p_info;
  1799. if (!prtd->lsm_client->use_topology) {
  1800. dev_err(rtd->dev,
  1801. "%s: %s: not supported if not using topology\n",
  1802. __func__, "SET_MODULE_PARAMS(_V2)_32");
  1803. err = -EINVAL;
  1804. goto done;
  1805. }
  1806. if (copy_from_user(&p_data_32, arg,
  1807. sizeof(p_data_32))) {
  1808. dev_err(rtd->dev,
  1809. "%s: %s: copy_from_user failed, size = %zd\n",
  1810. __func__, "SET_MODULE_PARAMS(_V2)_32",
  1811. sizeof(p_data_32));
  1812. err = -EFAULT;
  1813. goto done;
  1814. }
  1815. p_data.params = compat_ptr(p_data_32.params);
  1816. p_data.num_params = p_data_32.num_params;
  1817. p_data.data_size = p_data_32.data_size;
  1818. if (p_data.num_params > LSM_PARAMS_MAX) {
  1819. dev_err(rtd->dev,
  1820. "%s: %s: Invalid num_params %d\n",
  1821. __func__, "SET_MODULE_PARAMS(_V2)_32",
  1822. p_data.num_params);
  1823. err = -EINVAL;
  1824. goto done;
  1825. }
  1826. expected_size = (cmd == SNDRV_LSM_SET_MODULE_PARAMS_32) ?
  1827. p_data.num_params * sizeof(struct lsm_params_info_32) :
  1828. p_data.num_params * sizeof(struct lsm_params_info_v2_32);
  1829. if (p_data.data_size != expected_size) {
  1830. dev_err(rtd->dev,
  1831. "%s: %s: Invalid size %d\n",
  1832. __func__, "SET_MODULE_PARAMS(_V2)_32",
  1833. p_data.data_size);
  1834. err = -EINVAL;
  1835. goto done;
  1836. }
  1837. params32 = kzalloc(p_data.data_size, GFP_KERNEL);
  1838. if (!params32) {
  1839. err = -ENOMEM;
  1840. goto done;
  1841. }
  1842. if (copy_from_user(params32, p_data.params,
  1843. p_data.data_size)) {
  1844. dev_err(rtd->dev,
  1845. "%s: %s: copy_from_user failed, size = %d\n",
  1846. __func__, "params32", p_data.data_size);
  1847. kfree(params32);
  1848. err = -EFAULT;
  1849. goto done;
  1850. }
  1851. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS_32)
  1852. p_info_32 = (struct lsm_params_info_32 *) params32;
  1853. else
  1854. p_info_v2_32 = (struct lsm_params_info_v2_32 *) params32;
  1855. for (count = 0; count < p_data.num_params; count++) {
  1856. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS_32) {
  1857. p_info.module_id = p_info_32->module_id;
  1858. p_info.param_id = p_info_32->param_id;
  1859. p_info.param_size = p_info_32->param_size;
  1860. p_info.param_data = compat_ptr(p_info_32->param_data);
  1861. p_info.param_type = p_info_32->param_type;
  1862. p_info.instance_id = INSTANCE_ID_0;
  1863. p_info.stage_idx = LSM_STAGE_INDEX_FIRST;
  1864. p_info_32++;
  1865. } else {
  1866. p_info.module_id = p_info_v2_32->module_id;
  1867. p_info.param_id = p_info_v2_32->param_id;
  1868. p_info.param_size = p_info_v2_32->param_size;
  1869. p_info.param_data = compat_ptr(p_info_v2_32->param_data);
  1870. p_info.param_type = p_info_v2_32->param_type;
  1871. p_info.instance_id = p_info_v2_32->instance_id;
  1872. p_info.stage_idx = p_info_v2_32->stage_idx;
  1873. p_info_v2_32++;
  1874. }
  1875. err = msm_lsm_process_params(substream, &p_info);
  1876. if (err)
  1877. dev_err(rtd->dev,
  1878. "%s: Failed to process param, type%d stage=%d err=%d\n",
  1879. __func__, p_info.param_type, p_info.stage_idx, err);
  1880. }
  1881. kfree(params32);
  1882. break;
  1883. }
  1884. case SNDRV_LSM_REG_SND_MODEL_V2:
  1885. case SNDRV_LSM_SET_PARAMS:
  1886. case SNDRV_LSM_SET_MODULE_PARAMS:
  1887. case SNDRV_LSM_SET_MODULE_PARAMS_V2:
  1888. /*
  1889. * In ideal cases, the compat_ioctl should never be called
  1890. * with the above unlocked ioctl commands. Print error
  1891. * and return error if it does.
  1892. */
  1893. dev_err(rtd->dev,
  1894. "%s: Invalid cmd for compat_ioctl\n",
  1895. __func__);
  1896. err = -EINVAL;
  1897. break;
  1898. default:
  1899. err = msm_lsm_ioctl_shared(substream, cmd, arg);
  1900. break;
  1901. }
  1902. done:
  1903. mutex_unlock(&prtd->lsm_api_lock);
  1904. return err;
  1905. }
  1906. #endif /* CONFIG_AUDIO_QGKI */
  1907. #else
  1908. #define msm_lsm_ioctl_compat NULL
  1909. #endif
  1910. static int msm_lsm_ioctl(struct snd_pcm_substream *substream,
  1911. unsigned int cmd, void __user *arg)
  1912. {
  1913. int err = 0;
  1914. u32 size = 0;
  1915. struct snd_pcm_runtime *runtime;
  1916. struct snd_soc_pcm_runtime *rtd;
  1917. struct lsm_priv *prtd;
  1918. if (!substream || !substream->private_data) {
  1919. pr_err("%s: Invalid %s\n", __func__,
  1920. (!substream) ? "substream" : "private_data");
  1921. return -EINVAL;
  1922. }
  1923. runtime = substream->runtime;
  1924. prtd = runtime->private_data;
  1925. rtd = substream->private_data;
  1926. mutex_lock(&prtd->lsm_api_lock);
  1927. switch (cmd) {
  1928. case SNDRV_LSM_REG_SND_MODEL_V2: {
  1929. struct snd_lsm_sound_model_v2 snd_model_v2;
  1930. if (prtd->lsm_client->use_topology) {
  1931. dev_err(rtd->dev,
  1932. "%s: %s: not supported if using topology\n",
  1933. __func__, "REG_SND_MODEL_V2");
  1934. err = -EINVAL;
  1935. goto done;
  1936. }
  1937. if (copy_from_user(&snd_model_v2, arg, sizeof(snd_model_v2))) {
  1938. err = -EFAULT;
  1939. dev_err(rtd->dev,
  1940. "%s: copy from user failed, size %zd\n",
  1941. __func__,
  1942. sizeof(struct snd_lsm_sound_model_v2));
  1943. }
  1944. if (!err)
  1945. err = msm_lsm_ioctl_shared(substream, cmd,
  1946. &snd_model_v2);
  1947. if (err)
  1948. dev_err(rtd->dev,
  1949. "%s REG_SND_MODEL failed err %d\n",
  1950. __func__, err);
  1951. goto done;
  1952. }
  1953. break;
  1954. case SNDRV_LSM_SET_PARAMS: {
  1955. struct snd_lsm_detection_params det_params;
  1956. if (prtd->lsm_client->use_topology) {
  1957. dev_err(rtd->dev,
  1958. "%s: %s: not supported if using topology\n",
  1959. __func__, "SET_PARAMS");
  1960. err = -EINVAL;
  1961. goto done;
  1962. }
  1963. pr_debug("%s: SNDRV_LSM_SET_PARAMS\n", __func__);
  1964. if (copy_from_user(&det_params, arg,
  1965. sizeof(det_params))) {
  1966. dev_err(rtd->dev,
  1967. "%s: %s: copy_from_user failed, size %zd\n",
  1968. __func__, "SNDRV_LSM_SET_PARAMS",
  1969. sizeof(det_params));
  1970. err = -EFAULT;
  1971. }
  1972. if (!err)
  1973. err = msm_lsm_ioctl_shared(substream, cmd,
  1974. &det_params);
  1975. else
  1976. dev_err(rtd->dev,
  1977. "%s: LSM_SET_PARAMS failed, err %d\n",
  1978. __func__, err);
  1979. goto done;
  1980. }
  1981. case SNDRV_LSM_SET_MODULE_PARAMS:
  1982. case SNDRV_LSM_SET_MODULE_PARAMS_V2: {
  1983. struct snd_lsm_module_params p_data;
  1984. struct lsm_params_info *temp_ptr_info = NULL;
  1985. struct lsm_params_info_v2 info_v2;
  1986. struct lsm_params_info_v2 *ptr_info_v2 = NULL, *temp_ptr_info_v2 = NULL;
  1987. size_t p_size = 0, count;
  1988. u8 *params;
  1989. if (!prtd->lsm_client->use_topology) {
  1990. dev_err(rtd->dev,
  1991. "%s: %s: not supported if not using topology\n",
  1992. __func__, "SET_MODULE_PARAMS(_V2)");
  1993. err = -EINVAL;
  1994. goto done;
  1995. }
  1996. if (copy_from_user(&p_data, arg,
  1997. sizeof(p_data))) {
  1998. dev_err(rtd->dev,
  1999. "%s: %s: copy_from_user failed, size = %zd\n",
  2000. __func__, "p_data", sizeof(p_data));
  2001. err = -EFAULT;
  2002. goto done;
  2003. }
  2004. if (p_data.num_params > LSM_PARAMS_MAX) {
  2005. dev_err(rtd->dev,
  2006. "%s: %s: Invalid num_params %d\n",
  2007. __func__, "SET_MODULE_PARAMS(_V2)",
  2008. p_data.num_params);
  2009. err = -EINVAL;
  2010. goto done;
  2011. }
  2012. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS)
  2013. p_size = p_data.num_params * sizeof(struct lsm_params_info);
  2014. else
  2015. p_size = p_data.num_params * sizeof(struct lsm_params_info_v2);
  2016. if (p_data.data_size != p_size) {
  2017. dev_err(rtd->dev,
  2018. "%s: %s: Invalid data_size(%u) against expected(%zd)\n",
  2019. __func__, "SET_MODULE_PARAMS(_V2)",
  2020. p_data.data_size, p_size);
  2021. err = -EFAULT;
  2022. goto done;
  2023. }
  2024. params = kzalloc(p_size, GFP_KERNEL);
  2025. if (!params) {
  2026. err = -ENOMEM;
  2027. goto done;
  2028. }
  2029. if (copy_from_user(params, p_data.params,
  2030. p_data.data_size)) {
  2031. dev_err(rtd->dev,
  2032. "%s: %s: copy_from_user failed, size = %d\n",
  2033. __func__, "set module params", p_data.data_size);
  2034. kfree(params);
  2035. err = -EFAULT;
  2036. goto done;
  2037. }
  2038. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS)
  2039. temp_ptr_info = (struct lsm_params_info *)params;
  2040. else
  2041. temp_ptr_info_v2 = (struct lsm_params_info_v2 *)params;
  2042. for (count = 0; count < p_data.num_params; count++) {
  2043. if (cmd == SNDRV_LSM_SET_MODULE_PARAMS) {
  2044. /* convert to V2 param info struct from legacy param info */
  2045. info_v2.module_id = temp_ptr_info->module_id;
  2046. info_v2.param_id = temp_ptr_info->param_id;
  2047. info_v2.param_size = temp_ptr_info->param_size;
  2048. info_v2.param_data = temp_ptr_info->param_data;
  2049. info_v2.param_type = temp_ptr_info->param_type;
  2050. info_v2.instance_id = INSTANCE_ID_0;
  2051. info_v2.stage_idx = LSM_STAGE_INDEX_FIRST;
  2052. ptr_info_v2 = &info_v2;
  2053. temp_ptr_info++;
  2054. } else {
  2055. /* Just copy the pointer as user already provided v2 params */
  2056. ptr_info_v2 = temp_ptr_info_v2;
  2057. temp_ptr_info_v2++;
  2058. }
  2059. err = msm_lsm_process_params(substream, ptr_info_v2);
  2060. if (err)
  2061. dev_err(rtd->dev,
  2062. "%s: Failed to process param, type%d stage=%d err=%d\n",
  2063. __func__, ptr_info_v2->param_type,
  2064. ptr_info_v2->stage_idx, err);
  2065. }
  2066. kfree(params);
  2067. break;
  2068. }
  2069. case SNDRV_LSM_EVENT_STATUS:
  2070. case SNDRV_LSM_GENERIC_DET_EVENT: {
  2071. struct snd_lsm_event_status *user = NULL;
  2072. struct snd_lsm_event_status userarg;
  2073. dev_dbg(rtd->dev,
  2074. "%s: %s\n", __func__,
  2075. (cmd == SNDRV_LSM_EVENT_STATUS) ?
  2076. "SNDRV_LSM_EVENT_STATUS" :
  2077. "SNDRV_LSM_GENERIC_DET_EVENT");
  2078. err = msm_lsm_check_event_type(prtd->lsm_client, cmd);
  2079. if (err)
  2080. goto done;
  2081. if (copy_from_user(&userarg, arg, sizeof(userarg))) {
  2082. dev_err(rtd->dev,
  2083. "%s: %s: Copy from user failed\n", __func__,
  2084. (cmd == SNDRV_LSM_EVENT_STATUS) ?
  2085. "SNDRV_LSM_EVENT_STATUS" :
  2086. "SNDRV_LSM_GENERIC_DET_EVENT");
  2087. err = -EFAULT;
  2088. goto done;
  2089. }
  2090. if (userarg.payload_size >
  2091. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  2092. dev_err(rtd->dev,
  2093. "%s: payload_size %d is invalid, max allowed = %d\n",
  2094. __func__, userarg.payload_size,
  2095. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  2096. err = -EINVAL;
  2097. goto done;
  2098. }
  2099. size = sizeof(struct snd_lsm_event_status) +
  2100. userarg.payload_size;
  2101. user = kzalloc(size, GFP_KERNEL);
  2102. if (!user) {
  2103. err = -ENOMEM;
  2104. goto done;
  2105. }
  2106. user->payload_size = userarg.payload_size;
  2107. err = msm_lsm_ioctl_shared(substream, cmd, user);
  2108. if (err) {
  2109. dev_err(rtd->dev,
  2110. "%s: msm_lsm_ioctl_shared() failed, err = %d",
  2111. __func__, err);
  2112. kfree(user);
  2113. goto done;
  2114. }
  2115. /* Update size with actual payload size */
  2116. size = sizeof(*user) + user->payload_size;
  2117. if (!access_ok(arg, size)) {
  2118. dev_err(rtd->dev,
  2119. "%s: Failed to verify write, size = %d\n",
  2120. __func__, size);
  2121. err = -EFAULT;
  2122. }
  2123. if (!err && copy_to_user(arg, user, size)) {
  2124. dev_err(rtd->dev,
  2125. "%s: Failed to copy payload to user, size = %d\n",
  2126. __func__, size);
  2127. err = -EFAULT;
  2128. }
  2129. kfree(user);
  2130. break;
  2131. }
  2132. case SNDRV_LSM_EVENT_STATUS_V3: {
  2133. struct snd_lsm_event_status_v3 *user = NULL;
  2134. struct snd_lsm_event_status_v3 userarg;
  2135. dev_dbg(rtd->dev,
  2136. "%s: SNDRV_LSM_EVENT_STATUS_V3\n", __func__);
  2137. if (prtd->lsm_client->event_type !=
  2138. LSM_DET_EVENT_TYPE_LEGACY) {
  2139. dev_err(rtd->dev,
  2140. "%s: %s: Invalid event request\n",
  2141. __func__, "SNDRV_LSM_EVENT_STATUS_V3");
  2142. err = -EINVAL;
  2143. goto done;
  2144. }
  2145. if (!arg) {
  2146. dev_err(rtd->dev,
  2147. "%s: Invalid params event_status_v3\n",
  2148. __func__);
  2149. err = -EINVAL;
  2150. goto done;
  2151. }
  2152. if (copy_from_user(&userarg, arg, sizeof(userarg))) {
  2153. dev_err(rtd->dev,
  2154. "%s: err copyuser event_status_v3\n",
  2155. __func__);
  2156. err = -EFAULT;
  2157. goto done;
  2158. }
  2159. if (userarg.payload_size >
  2160. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  2161. pr_err("%s: payload_size %d is invalid, max allowed = %d\n",
  2162. __func__, userarg.payload_size,
  2163. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  2164. err = -EINVAL;
  2165. goto done;
  2166. }
  2167. size = sizeof(struct snd_lsm_event_status_v3) +
  2168. userarg.payload_size;
  2169. user = kzalloc(size, GFP_KERNEL);
  2170. if (!user) {
  2171. dev_err(rtd->dev,
  2172. "%s: Allocation failed event status size %d\n",
  2173. __func__, size);
  2174. err = -EFAULT;
  2175. goto done;
  2176. }
  2177. user->payload_size = userarg.payload_size;
  2178. err = msm_lsm_ioctl_shared(substream, cmd, user);
  2179. /* Update size with actual payload size */
  2180. size = sizeof(*user) + user->payload_size;
  2181. if (!err && !access_ok(arg, size)) {
  2182. dev_err(rtd->dev,
  2183. "%s: write verify failed size %d\n",
  2184. __func__, size);
  2185. err = -EFAULT;
  2186. }
  2187. if (!err && (copy_to_user(arg, user, size))) {
  2188. dev_err(rtd->dev,
  2189. "%s: failed to copy payload %d",
  2190. __func__, size);
  2191. err = -EFAULT;
  2192. }
  2193. kfree(user);
  2194. if (err)
  2195. dev_err(rtd->dev,
  2196. "%s: lsm_event_v3 failed %d", __func__, err);
  2197. break;
  2198. }
  2199. default:
  2200. err = msm_lsm_ioctl_shared(substream, cmd, arg);
  2201. break;
  2202. }
  2203. done:
  2204. mutex_unlock(&prtd->lsm_api_lock);
  2205. return err;
  2206. }
  2207. static int msm_lsm_open(struct snd_pcm_substream *substream)
  2208. {
  2209. struct snd_pcm_runtime *runtime = substream->runtime;
  2210. struct lsm_priv *prtd;
  2211. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2212. int ret = 0;
  2213. pr_debug("%s\n", __func__);
  2214. prtd = kzalloc(sizeof(struct lsm_priv), GFP_KERNEL);
  2215. if (!prtd) {
  2216. pr_err("%s: Failed to allocate memory for lsm_priv\n",
  2217. __func__);
  2218. return -ENOMEM;
  2219. }
  2220. mutex_init(&prtd->lsm_api_lock);
  2221. spin_lock_init(&prtd->event_lock);
  2222. spin_lock_init(&prtd->xrun_lock);
  2223. init_waitqueue_head(&prtd->event_wait);
  2224. init_waitqueue_head(&prtd->period_wait);
  2225. prtd->substream = substream;
  2226. runtime->private_data = prtd;
  2227. runtime->hw = msm_pcm_hardware_capture;
  2228. ret = snd_pcm_hw_constraint_list(runtime, 0,
  2229. SNDRV_PCM_HW_PARAM_RATE,
  2230. &constraints_sample_rates);
  2231. if (ret < 0)
  2232. pr_info("%s: snd_pcm_hw_constraint_list failed ret %d\n",
  2233. __func__, ret);
  2234. /* Ensure that buffer size is a multiple of period size */
  2235. ret = snd_pcm_hw_constraint_integer(runtime,
  2236. SNDRV_PCM_HW_PARAM_PERIODS);
  2237. if (ret < 0)
  2238. pr_info("%s: snd_pcm_hw_constraint_integer failed ret %d\n",
  2239. __func__, ret);
  2240. ret = snd_pcm_hw_constraint_minmax(runtime,
  2241. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  2242. CAPTURE_MIN_NUM_PERIODS * CAPTURE_MIN_PERIOD_SIZE,
  2243. CAPTURE_MAX_NUM_PERIODS * CAPTURE_MAX_PERIOD_SIZE);
  2244. if (ret < 0)
  2245. pr_info("%s: constraint for buffer bytes min max ret = %d\n",
  2246. __func__, ret);
  2247. ret = snd_pcm_hw_constraint_step(runtime, 0,
  2248. SNDRV_PCM_HW_PARAM_PERIOD_BYTES, 32);
  2249. if (ret < 0) {
  2250. pr_info("%s: constraint for period bytes step ret = %d\n",
  2251. __func__, ret);
  2252. }
  2253. ret = snd_pcm_hw_constraint_step(runtime, 0,
  2254. SNDRV_PCM_HW_PARAM_BUFFER_BYTES, 32);
  2255. if (ret < 0)
  2256. pr_info("%s: constraint for buffer bytes step ret = %d\n",
  2257. __func__, ret);
  2258. prtd->lsm_client = q6lsm_client_alloc(
  2259. lsm_event_handler, prtd);
  2260. if (!prtd->lsm_client) {
  2261. pr_err("%s: Could not allocate memory\n", __func__);
  2262. kfree(prtd);
  2263. runtime->private_data = NULL;
  2264. return -ENOMEM;
  2265. }
  2266. prtd->lsm_client->opened = false;
  2267. prtd->lsm_client->started = false;
  2268. prtd->lsm_client->session_state = IDLE;
  2269. prtd->lsm_client->poll_enable = true;
  2270. prtd->lsm_client->perf_mode = 0;
  2271. prtd->lsm_client->event_mode = LSM_EVENT_NON_TIME_STAMP_MODE;
  2272. prtd->lsm_client->event_type = LSM_DET_EVENT_TYPE_LEGACY;
  2273. prtd->lsm_client->fe_id = rtd->dai_link->id;
  2274. prtd->lsm_client->unprocessed_data = 0;
  2275. prtd->ws = wakeup_source_register(rtd->dev, "lsm-client");
  2276. return 0;
  2277. }
  2278. static int msm_lsm_send_ch_mix_config(struct snd_pcm_substream *substream)
  2279. {
  2280. struct snd_pcm_runtime *runtime = substream->runtime;
  2281. struct lsm_priv *prtd = runtime->private_data;
  2282. struct snd_soc_pcm_runtime *rtd;
  2283. struct lsm_hw_params *in_params;
  2284. int pp_ch_cnt;
  2285. int *ch_wght_coeff;
  2286. int ret = 0, i, idx;
  2287. /*
  2288. * The output channels from channel mixer is the input to LSM (stream)
  2289. * side and is read from in_params->num_chs.
  2290. *
  2291. * The input channels to channel mixer are the output channels from
  2292. * the device side (routing) and is obtained by reading the
  2293. * pp_ch_cnt.
  2294. *
  2295. * For LSM to be functional, only unity channel mixing is allowed.
  2296. */
  2297. in_params = &prtd->lsm_client->in_hw_params;
  2298. rtd = prtd->substream->private_data;
  2299. pp_ch_cnt = msm_pcm_routing_get_pp_ch_cnt(rtd->dai_link->id,
  2300. SESSION_TYPE_TX);
  2301. if (pp_ch_cnt < 0 ||
  2302. pp_ch_cnt > LSM_V3P0_MAX_NUM_CHANNELS ||
  2303. in_params->num_chs > LSM_V3P0_MAX_NUM_CHANNELS) {
  2304. dev_err(rtd->dev,
  2305. "%s: invalid ch cnt, pp_ch_cnt %d in_ch_cnt %d\n",
  2306. __func__, pp_ch_cnt, in_params->num_chs);
  2307. return -EINVAL;
  2308. }
  2309. if (!pp_ch_cnt ||
  2310. (pp_ch_cnt == in_params->num_chs)) {
  2311. dev_dbg(rtd->dev,
  2312. "%s: Skip ch mixing, pp_ch_cnt %d in_ch_cnt %d\n",
  2313. __func__, pp_ch_cnt, in_params->num_chs);
  2314. return 0;
  2315. }
  2316. ch_wght_coeff = kzalloc(in_params->num_chs * pp_ch_cnt * sizeof(int),
  2317. GFP_KERNEL);
  2318. if (!ch_wght_coeff)
  2319. return -ENOMEM;
  2320. /*
  2321. * channel weight co-efficients is a m X n array, where
  2322. * m = number of input channels to ch mixer (pp_ch_cnt)
  2323. * n = number of output channels from ch mixer (in_params->num_chs)
  2324. */
  2325. for (i = 0; i < in_params->num_chs; i++) {
  2326. idx = (i * pp_ch_cnt) + i;
  2327. ch_wght_coeff[idx] = 1;
  2328. }
  2329. ret = msm_pcm_routing_send_chmix_cfg(rtd->dai_link->id,
  2330. pp_ch_cnt, in_params->num_chs,
  2331. ch_wght_coeff,
  2332. SESSION_TYPE_TX, STREAM_TYPE_LSM);
  2333. if (ret)
  2334. dev_err(rtd->dev,
  2335. "%s: Failed to configure channel mixer err %d\n",
  2336. __func__, ret);
  2337. kfree(ch_wght_coeff);
  2338. return ret;
  2339. }
  2340. static int msm_lsm_prepare(struct snd_pcm_substream *substream)
  2341. {
  2342. struct snd_pcm_runtime *runtime = substream->runtime;
  2343. struct lsm_priv *prtd = runtime->private_data;
  2344. struct snd_soc_pcm_runtime *rtd;
  2345. int ret = 0;
  2346. if (!substream->private_data) {
  2347. pr_err("%s: Invalid private_data", __func__);
  2348. return -EINVAL;
  2349. }
  2350. rtd = prtd->substream->private_data;
  2351. if (!prtd->lsm_client) {
  2352. dev_err(rtd->dev,
  2353. "%s: LSM client data ptr is NULL\n", __func__);
  2354. return -EINVAL;
  2355. }
  2356. if (q6lsm_set_media_fmt_v2_params(prtd->lsm_client))
  2357. dev_dbg(rtd->dev,
  2358. "%s: failed to set lsm media fmt params\n", __func__);
  2359. if (prtd->lsm_client->session_state == IDLE) {
  2360. ret = msm_pcm_routing_reg_phy_compr_stream(
  2361. rtd->dai_link->id,
  2362. prtd->lsm_client->perf_mode,
  2363. prtd->lsm_client->session,
  2364. SNDRV_PCM_STREAM_CAPTURE,
  2365. LISTEN);
  2366. if (ret) {
  2367. dev_err(rtd->dev,
  2368. "%s: register phy compr stream failed %d\n",
  2369. __func__, ret);
  2370. return ret;
  2371. }
  2372. ret = msm_lsm_send_ch_mix_config(substream);
  2373. if (ret) {
  2374. msm_pcm_routing_dereg_phy_stream(rtd->dai_link->id,
  2375. SNDRV_PCM_STREAM_CAPTURE);
  2376. return ret;
  2377. }
  2378. }
  2379. prtd->lsm_client->session_state = RUNNING;
  2380. runtime->private_data = prtd;
  2381. return ret;
  2382. }
  2383. static int msm_lsm_close(struct snd_pcm_substream *substream)
  2384. {
  2385. unsigned long flags;
  2386. struct snd_pcm_runtime *runtime = substream->runtime;
  2387. struct lsm_priv *prtd = runtime->private_data;
  2388. struct snd_soc_pcm_runtime *rtd;
  2389. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  2390. int ret = 0;
  2391. int be_id = 0;
  2392. int fe_id = 0;
  2393. if (!substream->private_data) {
  2394. pr_err("%s: Invalid private_data", __func__);
  2395. return -EINVAL;
  2396. }
  2397. if (!prtd || !prtd->lsm_client) {
  2398. pr_err("%s: No LSM session active\n", __func__);
  2399. return -EINVAL;
  2400. }
  2401. rtd = substream->private_data;
  2402. dev_dbg(rtd->dev, "%s\n", __func__);
  2403. if (prtd->lsm_client->started) {
  2404. if (prtd->lsm_client->lab_enable) {
  2405. atomic_set(&prtd->read_abort, 1);
  2406. if (prtd->lsm_client->lab_started) {
  2407. ret = q6lsm_stop_lab(prtd->lsm_client);
  2408. if (ret)
  2409. dev_err(rtd->dev,
  2410. "%s: stop lab failed ret %d\n",
  2411. __func__, ret);
  2412. prtd->lsm_client->lab_started = false;
  2413. }
  2414. if (prtd->lsm_client->lab_buffer) {
  2415. ret = msm_lsm_lab_buffer_alloc(prtd,
  2416. LAB_BUFFER_DEALLOC);
  2417. if (ret)
  2418. dev_err(rtd->dev,
  2419. "%s: lab buffer dealloc failed ret %d\n",
  2420. __func__, ret);
  2421. }
  2422. }
  2423. if (!atomic_read(&prtd->read_abort)) {
  2424. dev_dbg(rtd->dev,
  2425. "%s: set read_abort to stop buffering\n", __func__);
  2426. atomic_set(&prtd->read_abort, 1);
  2427. }
  2428. ret = q6lsm_stop(prtd->lsm_client, true);
  2429. if (ret)
  2430. dev_err(rtd->dev,
  2431. "%s: session stop failed, err = %d\n",
  2432. __func__, ret);
  2433. else
  2434. dev_dbg(rtd->dev,
  2435. "%s: LSM client session stopped %d\n",
  2436. __func__, ret);
  2437. /*
  2438. * Go Ahead and try de-register sound model,
  2439. * even if stop failed
  2440. */
  2441. prtd->lsm_client->started = false;
  2442. ret = q6lsm_deregister_sound_model(prtd->lsm_client);
  2443. if (ret)
  2444. dev_err(rtd->dev,
  2445. "%s: dereg_snd_model failed, err = %d\n",
  2446. __func__, ret);
  2447. else
  2448. dev_dbg(rtd->dev, "%s: dereg_snd_model successful\n",
  2449. __func__);
  2450. }
  2451. msm_pcm_routing_dereg_phy_stream(rtd->dai_link->id,
  2452. SNDRV_PCM_STREAM_CAPTURE);
  2453. if (prtd->lsm_client->opened) {
  2454. if (!atomic_read(&prtd->read_abort)) {
  2455. dev_dbg(rtd->dev,
  2456. "%s: set read_abort to stop buffering\n", __func__);
  2457. atomic_set(&prtd->read_abort, 1);
  2458. }
  2459. q6lsm_close(prtd->lsm_client);
  2460. prtd->lsm_client->opened = false;
  2461. }
  2462. fe_id = prtd->lsm_client->fe_id;
  2463. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, SESSION_TYPE_TX,
  2464. &be_id, &cfg_data);
  2465. if (ret < 0)
  2466. dev_dbg(rtd->dev,
  2467. "%s: get stream app type cfg failed, err = %d\n",
  2468. __func__, ret);
  2469. /*
  2470. * be_id will be 0 in case of LSM directly connects to AFE due to
  2471. * last_be_id_configured[fedai_id][session_type] has not been updated.
  2472. * And then the cfg_data from wrong combination would be reset without
  2473. * this if check. We reset only if app_type, acdb_dev_id, and sample_rate
  2474. * are valid.
  2475. */
  2476. if (!cfg_data.app_type &&
  2477. !cfg_data.acdb_dev_id && !cfg_data.sample_rate) {
  2478. dev_dbg(rtd->dev, "%s: no need to reset app type configs\n",
  2479. __func__);
  2480. } else {
  2481. memset(&cfg_data, 0, sizeof(cfg_data));
  2482. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id,
  2483. SESSION_TYPE_TX,
  2484. be_id,
  2485. &cfg_data);
  2486. if (ret < 0)
  2487. dev_dbg(rtd->dev,
  2488. "%s: set stream app type cfg failed, err = %d\n",
  2489. __func__, ret);
  2490. }
  2491. q6lsm_client_free(prtd->lsm_client);
  2492. wakeup_source_unregister(prtd->ws);
  2493. spin_lock_irqsave(&prtd->event_lock, flags);
  2494. kfree(prtd->event_status);
  2495. prtd->event_status = NULL;
  2496. kfree(prtd->det_event);
  2497. prtd->det_event = NULL;
  2498. spin_unlock_irqrestore(&prtd->event_lock, flags);
  2499. mutex_destroy(&prtd->lsm_api_lock);
  2500. kfree(prtd);
  2501. runtime->private_data = NULL;
  2502. return 0;
  2503. }
  2504. static int msm_lsm_hw_params(struct snd_pcm_substream *substream,
  2505. struct snd_pcm_hw_params *params)
  2506. {
  2507. struct snd_pcm_runtime *runtime = substream->runtime;
  2508. struct lsm_priv *prtd = runtime->private_data;
  2509. struct lsm_hw_params *out_hw_params = NULL;
  2510. struct lsm_hw_params *in_hw_params = NULL;
  2511. struct snd_soc_pcm_runtime *rtd;
  2512. if (!substream->private_data) {
  2513. pr_err("%s: Invalid private_data", __func__);
  2514. return -EINVAL;
  2515. }
  2516. rtd = substream->private_data;
  2517. if (!prtd || !params) {
  2518. dev_err(rtd->dev,
  2519. "%s: invalid params prtd %pK params %pK",
  2520. __func__, prtd, params);
  2521. return -EINVAL;
  2522. }
  2523. in_hw_params = &prtd->lsm_client->in_hw_params;
  2524. out_hw_params = &prtd->lsm_client->out_hw_params;
  2525. out_hw_params->num_chs = params_channels(params);
  2526. out_hw_params->period_count = params_periods(params);
  2527. out_hw_params->sample_rate = params_rate(params);
  2528. if (((out_hw_params->sample_rate != 16000) &&
  2529. (out_hw_params->sample_rate != 48000)) ||
  2530. (out_hw_params->period_count == 0)) {
  2531. dev_err(rtd->dev,
  2532. "%s: Invalid Params sample rate %d period count %d\n",
  2533. __func__, out_hw_params->sample_rate,
  2534. out_hw_params->period_count);
  2535. return -EINVAL;
  2536. }
  2537. if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE) {
  2538. out_hw_params->sample_size = 16;
  2539. } else if (params_format(params) == SNDRV_PCM_FORMAT_S24_LE) {
  2540. out_hw_params->sample_size = 24;
  2541. } else {
  2542. dev_err(rtd->dev, "%s: Invalid Format 0x%x\n",
  2543. __func__, params_format(params));
  2544. return -EINVAL;
  2545. }
  2546. out_hw_params->buf_sz = params_buffer_bytes(params) /
  2547. out_hw_params->period_count;
  2548. dev_dbg(rtd->dev,
  2549. "%s: channels %d sample rate %d sample size %d buffer size %d period count %d\n",
  2550. __func__, out_hw_params->num_chs, out_hw_params->sample_rate,
  2551. out_hw_params->sample_size, out_hw_params->buf_sz,
  2552. out_hw_params->period_count);
  2553. /*
  2554. * copy the out_hw_params to in_hw_params. in_hw_params will be
  2555. * over-written with LSM_SET_INPUT_HW_PARAMS ioctl from userspace.
  2556. * If this ioctl is not set, then it is assumed that input and
  2557. * output hw params for LSM are the same.
  2558. * Currently the period_count and buf_sz are unused for input params.
  2559. */
  2560. memcpy(in_hw_params, out_hw_params,
  2561. sizeof(struct lsm_hw_params));
  2562. return 0;
  2563. }
  2564. static snd_pcm_uframes_t msm_lsm_pcm_pointer(
  2565. struct snd_pcm_substream *substream)
  2566. {
  2567. struct snd_pcm_runtime *runtime = substream->runtime;
  2568. struct lsm_priv *prtd = runtime->private_data;
  2569. struct snd_soc_pcm_runtime *rtd;
  2570. if (!substream->private_data) {
  2571. pr_err("%s: Invalid private_data", __func__);
  2572. return -EINVAL;
  2573. }
  2574. rtd = substream->private_data;
  2575. if (!prtd) {
  2576. dev_err(rtd->dev,
  2577. "%s: Invalid param %pK\n", __func__, prtd);
  2578. return 0;
  2579. }
  2580. if (prtd->dma_write >= snd_pcm_lib_buffer_bytes(substream))
  2581. prtd->dma_write = 0;
  2582. dev_dbg(rtd->dev,
  2583. "%s: dma post = %d\n", __func__, prtd->dma_write);
  2584. return bytes_to_frames(runtime, prtd->dma_write);
  2585. }
  2586. static int msm_lsm_pcm_copy(struct snd_pcm_substream *substream, int ch,
  2587. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  2588. {
  2589. struct snd_pcm_runtime *runtime = substream->runtime;
  2590. struct lsm_priv *prtd = runtime->private_data;
  2591. char *pcm_buf = NULL;
  2592. int rc = 0, buf_index = 0;
  2593. unsigned long flags = 0;
  2594. struct snd_soc_pcm_runtime *rtd;
  2595. if (!substream->private_data) {
  2596. pr_err("%s: Invalid private_data", __func__);
  2597. return -EINVAL;
  2598. }
  2599. rtd = substream->private_data;
  2600. if (!prtd) {
  2601. dev_err(rtd->dev,
  2602. "%s: Invalid param %pK\n", __func__, prtd);
  2603. return -EINVAL;
  2604. }
  2605. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  2606. runtime->status->state == SNDRV_PCM_STATE_PREPARED) {
  2607. dev_err(rtd->dev,
  2608. "%s: runtime state incorrect %d", __func__,
  2609. runtime->status->state);
  2610. return 0;
  2611. }
  2612. rc = wait_event_timeout(prtd->period_wait,
  2613. (atomic_read(&prtd->buf_count) |
  2614. atomic_read(&prtd->read_abort)), (2 * HZ));
  2615. if (!rc) {
  2616. dev_err(rtd->dev,
  2617. "%s: timeout for read retry\n", __func__);
  2618. return -EAGAIN;
  2619. }
  2620. if (atomic_read(&prtd->read_abort)) {
  2621. dev_err(rtd->dev,
  2622. "%s: Read abort received\n", __func__);
  2623. return -EIO;
  2624. }
  2625. prtd->appl_cnt = prtd->appl_cnt %
  2626. prtd->lsm_client->out_hw_params.period_count;
  2627. pcm_buf = prtd->lsm_client->lab_buffer[prtd->appl_cnt].data;
  2628. dev_dbg(rtd->dev,
  2629. "%s: copy the pcm data size %lu\n",
  2630. __func__, fbytes);
  2631. if (pcm_buf) {
  2632. if (copy_to_user(buf, pcm_buf, fbytes)) {
  2633. dev_err(rtd->dev,
  2634. "%s: failed to copy bytes %lu\n",
  2635. __func__, fbytes);
  2636. return -EINVAL;
  2637. }
  2638. } else {
  2639. dev_err(rtd->dev,
  2640. "%s: Invalid pcm buffer\n", __func__);
  2641. return -EINVAL;
  2642. }
  2643. prtd->appl_cnt = (prtd->appl_cnt + 1) %
  2644. prtd->lsm_client->out_hw_params.period_count;
  2645. spin_lock_irqsave(&prtd->xrun_lock, flags);
  2646. /* Queue lab buffer here if in xrun */
  2647. if (prtd->xrun_count > 0) {
  2648. (prtd->xrun_count)--;
  2649. buf_index = (prtd->xrun_index + 1) %
  2650. prtd->lsm_client->out_hw_params.period_count;
  2651. rc = msm_lsm_queue_lab_buffer(prtd, buf_index);
  2652. if (rc)
  2653. dev_err(rtd->dev,
  2654. "%s: error in queuing the lab buffer rc %d\n",
  2655. __func__, rc);
  2656. prtd->xrun_index = buf_index;
  2657. }
  2658. atomic_dec(&prtd->buf_count);
  2659. spin_unlock_irqrestore(&prtd->xrun_lock, flags);
  2660. return 0;
  2661. }
  2662. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2663. static int msm_lsm_app_type_cfg_ctl_put(struct snd_kcontrol *kcontrol,
  2664. struct snd_ctl_elem_value *ucontrol)
  2665. {
  2666. u64 fe_id = kcontrol->private_value;
  2667. int session_type = SESSION_TYPE_TX;
  2668. int be_id = ucontrol->value.integer.value[3];
  2669. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  2670. int ret = 0;
  2671. cfg_data.app_type = ucontrol->value.integer.value[0];
  2672. cfg_data.acdb_dev_id = ucontrol->value.integer.value[1];
  2673. cfg_data.sample_rate = ucontrol->value.integer.value[2];
  2674. pr_debug("%s: fe_id- %llu session_type- %d be_id- %d app_type- %d acdb_dev_id- %d sample_rate- %d\n",
  2675. __func__, fe_id, session_type, be_id,
  2676. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  2677. ret = msm_pcm_routing_reg_stream_app_type_cfg(fe_id, session_type,
  2678. be_id, &cfg_data);
  2679. if (ret < 0)
  2680. pr_err("%s: msm_pcm_routing_reg_stream_app_type_cfg failed returned %d\n",
  2681. __func__, ret);
  2682. return 0;
  2683. }
  2684. static int msm_lsm_app_type_cfg_ctl_get(struct snd_kcontrol *kcontrol,
  2685. struct snd_ctl_elem_value *ucontrol)
  2686. {
  2687. u64 fe_id = kcontrol->private_value;
  2688. int session_type = SESSION_TYPE_TX;
  2689. int be_id = 0;
  2690. struct msm_pcm_stream_app_type_cfg cfg_data = {0};
  2691. int ret = 0;
  2692. ret = msm_pcm_routing_get_stream_app_type_cfg(fe_id, session_type,
  2693. &be_id, &cfg_data);
  2694. if (ret < 0) {
  2695. pr_err("%s: msm_pcm_routing_get_stream_app_type_cfg failed returned %d\n",
  2696. __func__, ret);
  2697. goto done;
  2698. }
  2699. ucontrol->value.integer.value[0] = cfg_data.app_type;
  2700. ucontrol->value.integer.value[1] = cfg_data.acdb_dev_id;
  2701. ucontrol->value.integer.value[2] = cfg_data.sample_rate;
  2702. ucontrol->value.integer.value[3] = be_id;
  2703. pr_debug("%s: fedai_id %llu, session_type %d, be_id %d, app_type %d, acdb_dev_id %d, sample_rate %d\n",
  2704. __func__, fe_id, session_type, be_id,
  2705. cfg_data.app_type, cfg_data.acdb_dev_id, cfg_data.sample_rate);
  2706. done:
  2707. return ret;
  2708. }
  2709. static int msm_lsm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  2710. {
  2711. struct snd_pcm *pcm = rtd->pcm;
  2712. struct snd_pcm_usr *app_type_info;
  2713. struct snd_kcontrol *kctl;
  2714. const char *mixer_ctl_name = "Listen Stream";
  2715. const char *deviceNo = "NN";
  2716. const char *suffix = "App Type Cfg";
  2717. int ctl_len, ret = 0;
  2718. ctl_len = strlen(mixer_ctl_name) + 1 +
  2719. strlen(deviceNo) + 1 + strlen(suffix) + 1;
  2720. pr_debug("%s: Listen app type cntrl add\n", __func__);
  2721. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  2722. NULL, 1, ctl_len, rtd->dai_link->id,
  2723. &app_type_info);
  2724. if (ret < 0) {
  2725. pr_err("%s: Listen app type cntrl add failed: %d\n",
  2726. __func__, ret);
  2727. return ret;
  2728. }
  2729. kctl = app_type_info->kctl;
  2730. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  2731. mixer_ctl_name, rtd->pcm->device, suffix);
  2732. kctl->put = msm_lsm_app_type_cfg_ctl_put;
  2733. kctl->get = msm_lsm_app_type_cfg_ctl_get;
  2734. return 0;
  2735. }
  2736. #else
  2737. static int msm_lsm_add_app_type_controls(struct snd_soc_pcm_runtime *rtd)
  2738. {
  2739. return 0;
  2740. }
  2741. #endif /* CONFIG_AUDIO_QGKI */
  2742. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2743. static int msm_lsm_afe_data_ctl_put(struct snd_kcontrol *kcontrol,
  2744. struct snd_ctl_elem_value *ucontrol)
  2745. {
  2746. u64 fe_id = kcontrol->private_value;
  2747. uint16_t afe_data_format = 0;
  2748. int ret = 0;
  2749. afe_data_format = ucontrol->value.integer.value[0];
  2750. pr_debug("%s: afe data is %s\n", __func__,
  2751. afe_data_format ? "unprocessed" : "processed");
  2752. ret = q6lsm_set_afe_data_format(fe_id, afe_data_format);
  2753. if (ret)
  2754. pr_err("%s: q6lsm_set_afe_data_format failed, ret = %d\n",
  2755. __func__, ret);
  2756. return ret;
  2757. }
  2758. static int msm_lsm_afe_data_ctl_get(struct snd_kcontrol *kcontrol,
  2759. struct snd_ctl_elem_value *ucontrol)
  2760. {
  2761. u64 fe_id = kcontrol->private_value;
  2762. uint16_t afe_data_format = 0;
  2763. int ret = 0;
  2764. q6lsm_get_afe_data_format(fe_id, &afe_data_format);
  2765. ucontrol->value.integer.value[0] = afe_data_format;
  2766. pr_debug("%s: afe data is %s\n", __func__,
  2767. afe_data_format ? "unprocessed" : "processed");
  2768. return ret;
  2769. }
  2770. static int msm_lsm_add_afe_data_controls(struct snd_soc_pcm_runtime *rtd)
  2771. {
  2772. struct snd_pcm *pcm = rtd->pcm;
  2773. struct snd_pcm_usr *afe_data_info;
  2774. struct snd_kcontrol *kctl;
  2775. const char *mixer_ctl_name = "Listen Stream";
  2776. const char *deviceNo = "NN";
  2777. const char *suffix = "Unprocessed Data";
  2778. int ctl_len, ret = 0;
  2779. ctl_len = strlen(mixer_ctl_name) + 1 + strlen(deviceNo) + 1 +
  2780. strlen(suffix) + 1;
  2781. pr_debug("%s: Adding Listen afe data cntrls\n", __func__);
  2782. ret = snd_pcm_add_usr_ctls(pcm, SNDRV_PCM_STREAM_CAPTURE,
  2783. NULL, 1, ctl_len, rtd->dai_link->id,
  2784. &afe_data_info);
  2785. if (ret < 0) {
  2786. pr_err("%s: Adding Listen afe data cntrls failed: %d\n",
  2787. __func__, ret);
  2788. return ret;
  2789. }
  2790. kctl = afe_data_info->kctl;
  2791. snprintf(kctl->id.name, ctl_len, "%s %d %s",
  2792. mixer_ctl_name, rtd->pcm->device, suffix);
  2793. kctl->put = msm_lsm_afe_data_ctl_put;
  2794. kctl->get = msm_lsm_afe_data_ctl_get;
  2795. return 0;
  2796. }
  2797. #else
  2798. static int msm_lsm_add_afe_data_controls(struct snd_soc_pcm_runtime *rtd)
  2799. {
  2800. return 0;
  2801. }
  2802. #endif /* CONFIG_AUDIO_QGKI */
  2803. static int msm_lsm_add_controls(struct snd_soc_pcm_runtime *rtd)
  2804. {
  2805. int ret = 0;
  2806. ret = msm_lsm_add_app_type_controls(rtd);
  2807. if (ret)
  2808. pr_err("%s, add app type controls failed:%d\n", __func__, ret);
  2809. ret = msm_lsm_add_afe_data_controls(rtd);
  2810. if (ret)
  2811. pr_err("%s, add afe data controls failed:%d\n", __func__, ret);
  2812. return ret;
  2813. }
  2814. static const struct snd_pcm_ops msm_lsm_ops = {
  2815. .open = msm_lsm_open,
  2816. .close = msm_lsm_close,
  2817. .ioctl = msm_lsm_ioctl,
  2818. .prepare = msm_lsm_prepare,
  2819. #if IS_ENABLED(CONFIG_AUDIO_QGKI)
  2820. .compat_ioctl = msm_lsm_ioctl_compat,
  2821. #endif /* CONFIG_AUDIO_QGKI */
  2822. .hw_params = msm_lsm_hw_params,
  2823. .copy_user = msm_lsm_pcm_copy,
  2824. .pointer = msm_lsm_pcm_pointer,
  2825. };
  2826. static int msm_asoc_lsm_new(struct snd_soc_pcm_runtime *rtd)
  2827. {
  2828. struct snd_card *card = rtd->card->snd_card;
  2829. int ret = 0;
  2830. if (!card->dev->coherent_dma_mask)
  2831. card->dev->coherent_dma_mask = DMA_BIT_MASK(32);
  2832. ret = msm_lsm_add_controls(rtd);
  2833. if (ret)
  2834. pr_err("%s, kctl add failed:%d\n", __func__, ret);
  2835. return ret;
  2836. }
  2837. static int msm_asoc_lsm_probe(struct snd_soc_component *component)
  2838. {
  2839. pr_debug("enter %s\n", __func__);
  2840. return 0;
  2841. }
  2842. static struct snd_soc_component_driver msm_soc_component = {
  2843. .name = DRV_NAME,
  2844. .ops = &msm_lsm_ops,
  2845. .pcm_new = msm_asoc_lsm_new,
  2846. .probe = msm_asoc_lsm_probe,
  2847. };
  2848. static int msm_lsm_probe(struct platform_device *pdev)
  2849. {
  2850. return snd_soc_register_component(&pdev->dev, &msm_soc_component,
  2851. NULL, 0);
  2852. }
  2853. static int msm_lsm_remove(struct platform_device *pdev)
  2854. {
  2855. snd_soc_unregister_component(&pdev->dev);
  2856. return 0;
  2857. }
  2858. static const struct of_device_id msm_lsm_client_dt_match[] = {
  2859. {.compatible = "qcom,msm-lsm-client" },
  2860. { }
  2861. };
  2862. static struct platform_driver msm_lsm_driver = {
  2863. .driver = {
  2864. .name = "msm-lsm-client",
  2865. .owner = THIS_MODULE,
  2866. .of_match_table = of_match_ptr(msm_lsm_client_dt_match),
  2867. .suppress_bind_attrs = true,
  2868. },
  2869. .probe = msm_lsm_probe,
  2870. .remove = msm_lsm_remove,
  2871. };
  2872. int __init msm_lsm_client_init(void)
  2873. {
  2874. return platform_driver_register(&msm_lsm_driver);
  2875. }
  2876. void msm_lsm_client_exit(void)
  2877. {
  2878. platform_driver_unregister(&msm_lsm_driver);
  2879. }
  2880. MODULE_DESCRIPTION("LSM client platform driver");
  2881. MODULE_DEVICE_TABLE(of, msm_lsm_client_dt_match);
  2882. MODULE_LICENSE("GPL v2");