msm-lsm-client.c 85 KB

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