msm-lsm-client.c 85 KB

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