msm-lsm-client.c 86 KB

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