msm-lsm-client.c 80 KB

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