msm-lsm-client.c 80 KB

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