msm-lsm-client.c 94 KB

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