msm-cpe-lsm.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355
  1. /*
  2. * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/device.h>
  16. #include <linux/slab.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/kthread.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/of.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include <linux/freezer.h>
  24. #include <sound/soc.h>
  25. #include <sound/lsm_params.h>
  26. #include <sound/pcm_params.h>
  27. #include "msm-slim-dma.h"
  28. #include "codecs/cpe_core.h"
  29. #define SAMPLE_RATE_48KHZ 48000
  30. #define SAMPLE_RATE_16KHZ 16000
  31. #define LSM_VOICE_WAKEUP_APP_V2 2
  32. #define AFE_PORT_ID_1 1
  33. #define AFE_PORT_ID_3 3
  34. #define AFE_OUT_PORT_2 2
  35. #define LISTEN_MIN_NUM_PERIODS 2
  36. #define LISTEN_MAX_NUM_PERIODS 12
  37. #define LISTEN_MAX_PERIOD_SIZE 61440
  38. #define LISTEN_MIN_PERIOD_SIZE 320
  39. #define LISTEN_MAX_STATUS_PAYLOAD_SIZE 256
  40. #define MSM_CPE_MAX_CUSTOM_PARAM_SIZE 2048
  41. #define MSM_CPE_LAB_THREAD_TIMEOUT (3 * (HZ/10))
  42. #define MSM_CPE_LSM_GRAB_LOCK(lock, name) \
  43. { \
  44. pr_debug("%s: %s lock acquire\n", \
  45. __func__, name); \
  46. mutex_lock(lock); \
  47. }
  48. #define MSM_CPE_LSM_REL_LOCK(lock, name) \
  49. { \
  50. pr_debug("%s: %s lock release\n", \
  51. __func__, name); \
  52. mutex_unlock(lock); \
  53. }
  54. /* Conventional and unconventional sample rate supported */
  55. static unsigned int supported_sample_rates[] = {
  56. 8000, 16000, 48000, 192000, 384000
  57. };
  58. static struct snd_pcm_hw_constraint_list constraints_sample_rates = {
  59. .count = ARRAY_SIZE(supported_sample_rates),
  60. .list = supported_sample_rates,
  61. .mask = 0,
  62. };
  63. static struct snd_pcm_hardware msm_pcm_hardware_listen = {
  64. .info = (SNDRV_PCM_INFO_BLOCK_TRANSFER |
  65. SNDRV_PCM_INFO_MMAP_VALID |
  66. SNDRV_PCM_INFO_INTERLEAVED |
  67. SNDRV_PCM_INFO_PAUSE |
  68. SNDRV_PCM_INFO_RESUME),
  69. .formats = (SNDRV_PCM_FMTBIT_S16_LE |
  70. SNDRV_PCM_FMTBIT_S24_LE |
  71. SNDRV_PCM_FMTBIT_S32_LE),
  72. .rates = (SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_48000 |
  73. SNDRV_PCM_RATE_192000 | SNDRV_PCM_RATE_384000),
  74. .rate_min = 16000,
  75. .rate_max = 384000,
  76. .channels_min = 1,
  77. .channels_max = 1,
  78. .buffer_bytes_max = LISTEN_MAX_NUM_PERIODS *
  79. LISTEN_MAX_PERIOD_SIZE,
  80. .period_bytes_min = LISTEN_MIN_PERIOD_SIZE,
  81. .period_bytes_max = LISTEN_MAX_PERIOD_SIZE,
  82. .periods_min = LISTEN_MIN_NUM_PERIODS,
  83. .periods_max = LISTEN_MAX_NUM_PERIODS,
  84. .fifo_size = 0,
  85. };
  86. enum {
  87. AFE_CMD_INVALID = 0,
  88. AFE_CMD_PORT_START,
  89. AFE_CMD_PORT_SUSPEND,
  90. AFE_CMD_PORT_RESUME,
  91. AFE_CMD_PORT_STOP,
  92. };
  93. enum cpe_lab_thread_status {
  94. MSM_LSM_LAB_THREAD_STOP,
  95. MSM_LSM_LAB_THREAD_RUNNING,
  96. MSM_LSM_LAB_THREAD_ERROR,
  97. };
  98. struct cpe_hw_params {
  99. u32 sample_rate;
  100. u16 sample_size;
  101. u32 buf_sz;
  102. u32 period_count;
  103. u16 channels;
  104. };
  105. struct cpe_data_pcm_buf {
  106. u8 *mem;
  107. phys_addr_t phys;
  108. };
  109. struct cpe_lsm_lab {
  110. atomic_t in_count;
  111. atomic_t abort_read;
  112. u32 dma_write;
  113. u32 buf_idx;
  114. u32 pcm_size;
  115. enum cpe_lab_thread_status thread_status;
  116. struct cpe_data_pcm_buf *pcm_buf;
  117. wait_queue_head_t period_wait;
  118. struct completion comp;
  119. struct completion thread_complete;
  120. };
  121. struct cpe_priv {
  122. void *core_handle;
  123. struct snd_soc_codec *codec;
  124. struct wcd_cpe_lsm_ops lsm_ops;
  125. struct wcd_cpe_afe_ops afe_ops;
  126. bool afe_mad_ctl;
  127. u32 input_port_id;
  128. };
  129. struct cpe_lsm_data {
  130. struct device *dev;
  131. struct cpe_lsm_session *lsm_session;
  132. struct mutex lsm_api_lock;
  133. struct cpe_lsm_lab lab;
  134. struct cpe_hw_params hw_params;
  135. struct snd_pcm_substream *substream;
  136. wait_queue_head_t event_wait;
  137. atomic_t event_avail;
  138. atomic_t event_stop;
  139. u8 ev_det_status;
  140. u8 ev_det_pld_size;
  141. u8 *ev_det_payload;
  142. bool cpe_prepared;
  143. };
  144. static int msm_cpe_afe_mad_ctl_get(struct snd_kcontrol *kcontrol,
  145. struct snd_ctl_elem_value *ucontrol)
  146. {
  147. struct cpe_priv *cpe = kcontrol->private_data;
  148. ucontrol->value.integer.value[0] = cpe->afe_mad_ctl;
  149. return 0;
  150. }
  151. static int msm_cpe_afe_mad_ctl_put(struct snd_kcontrol *kcontrol,
  152. struct snd_ctl_elem_value *ucontrol)
  153. {
  154. struct cpe_priv *cpe = kcontrol->private_data;
  155. cpe->afe_mad_ctl = ucontrol->value.integer.value[0];
  156. return 0;
  157. }
  158. static struct snd_kcontrol_new msm_cpe_kcontrols[] = {
  159. SOC_SINGLE_EXT("CPE AFE MAD Enable", SND_SOC_NOPM, 0, 1, 0,
  160. msm_cpe_afe_mad_ctl_get, msm_cpe_afe_mad_ctl_put),
  161. };
  162. /*
  163. * cpe_get_private_data: obtain ASoC platform driver private data
  164. * @substream: ASoC substream for which private data to be obtained
  165. */
  166. static struct cpe_priv *cpe_get_private_data(
  167. struct snd_pcm_substream *substream)
  168. {
  169. struct snd_soc_pcm_runtime *rtd;
  170. if (!substream || !substream->private_data) {
  171. pr_err("%s: %s is invalid\n",
  172. __func__,
  173. (!substream) ? "substream" : "private_data");
  174. goto err_ret;
  175. }
  176. rtd = substream->private_data;
  177. if (!rtd || !rtd->platform) {
  178. pr_err("%s: %s is invalid\n",
  179. __func__,
  180. (!rtd) ? "runtime" : "platform");
  181. goto err_ret;
  182. }
  183. return snd_soc_platform_get_drvdata(rtd->platform);
  184. err_ret:
  185. return NULL;
  186. }
  187. /*
  188. * cpe_get_lsm_data: obtain the lsm session data given the substream
  189. * @substream: ASoC substream for which lsm session data to be obtained
  190. */
  191. static struct cpe_lsm_data *cpe_get_lsm_data(
  192. struct snd_pcm_substream *substream)
  193. {
  194. struct snd_pcm_runtime *runtime = substream->runtime;
  195. return runtime->private_data;
  196. }
  197. static void msm_cpe_process_event_status(void *data,
  198. u8 detect_status, u8 size, u8 *payload)
  199. {
  200. struct cpe_lsm_data *lsm_d = data;
  201. lsm_d->ev_det_status = detect_status;
  202. lsm_d->ev_det_pld_size = size;
  203. lsm_d->ev_det_payload = kzalloc(size, GFP_KERNEL);
  204. if (!lsm_d->ev_det_payload)
  205. return;
  206. memcpy(lsm_d->ev_det_payload, payload, size);
  207. atomic_set(&lsm_d->event_avail, 1);
  208. wake_up(&lsm_d->event_wait);
  209. }
  210. static void msm_cpe_process_event_status_done(struct cpe_lsm_data *lsm_data)
  211. {
  212. kfree(lsm_data->ev_det_payload);
  213. lsm_data->ev_det_payload = NULL;
  214. lsm_data->ev_det_status = 0;
  215. lsm_data->ev_det_pld_size = 0;
  216. }
  217. /*
  218. * msm_cpe_afe_port_cntl: Perform the afe port control
  219. * @substream: substream for which afe port command to be performed
  220. * @core_handle: handle to core
  221. * @afe_ops: handle to the afe operations
  222. * @afe_cfg: afe port configuration data
  223. * @cmd: command to be sent to AFE
  224. *
  225. */
  226. static int msm_cpe_afe_port_cntl(
  227. struct snd_pcm_substream *substream,
  228. void *core_handle,
  229. struct wcd_cpe_afe_ops *afe_ops,
  230. struct wcd_cpe_afe_port_cfg *afe_cfg,
  231. int cmd)
  232. {
  233. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  234. int rc = 0;
  235. if (!afe_cfg->port_id) {
  236. /*
  237. * It is possible driver can get closed without prepare,
  238. * in which case afe ports will not be initialized.
  239. */
  240. dev_dbg(rtd->dev,
  241. "%s: Invalid afe port id\n",
  242. __func__);
  243. return 0;
  244. }
  245. switch (cmd) {
  246. case AFE_CMD_PORT_START:
  247. rc = afe_ops->afe_port_start(core_handle, afe_cfg);
  248. if (rc != 0)
  249. dev_err(rtd->dev,
  250. "%s: AFE port start failed\n",
  251. __func__);
  252. break;
  253. case AFE_CMD_PORT_SUSPEND:
  254. rc = afe_ops->afe_port_suspend(core_handle, afe_cfg);
  255. if (rc != 0)
  256. dev_err(rtd->dev,
  257. "%s: afe_suspend failed, err = %d\n",
  258. __func__, rc);
  259. break;
  260. case AFE_CMD_PORT_RESUME:
  261. rc = afe_ops->afe_port_resume(core_handle, afe_cfg);
  262. if (rc != 0)
  263. dev_err(rtd->dev,
  264. "%s: afe_resume failed, err = %d\n",
  265. __func__, rc);
  266. break;
  267. case AFE_CMD_PORT_STOP:
  268. rc = afe_ops->afe_port_stop(core_handle, afe_cfg);
  269. if (rc != 0)
  270. dev_err(rtd->dev,
  271. "%s: afe_stopfailed, err = %d\n",
  272. __func__, rc);
  273. break;
  274. }
  275. return rc;
  276. }
  277. static int msm_cpe_lsm_lab_stop(struct snd_pcm_substream *substream)
  278. {
  279. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  280. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  281. struct cpe_priv *cpe = cpe_get_private_data(substream);
  282. struct wcd_cpe_lsm_ops *lsm_ops;
  283. struct wcd_cpe_afe_ops *afe_ops;
  284. struct cpe_lsm_session *session;
  285. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  286. struct msm_slim_dma_data *dma_data = NULL;
  287. int rc;
  288. /*
  289. * the caller is not aware of LAB status and will
  290. * try to stop lab even if it is already stopped.
  291. * return success right away is LAB is already stopped
  292. */
  293. if (lab_d->thread_status == MSM_LSM_LAB_THREAD_STOP) {
  294. dev_dbg(rtd->dev,
  295. "%s: lab already stopped\n",
  296. __func__);
  297. return 0;
  298. }
  299. if (!cpe || !cpe->core_handle) {
  300. dev_err(rtd->dev,
  301. "%s: Invalid private data\n",
  302. __func__);
  303. return -EINVAL;
  304. }
  305. if (!lsm_d->lsm_session) {
  306. dev_err(rtd->dev,
  307. "%s: Invalid session data\n",
  308. __func__);
  309. return -EINVAL;
  310. }
  311. lsm_ops = &cpe->lsm_ops;
  312. afe_ops = &cpe->afe_ops;
  313. session = lsm_d->lsm_session;
  314. if (rtd->cpu_dai)
  315. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
  316. substream);
  317. if (!dma_data || !dma_data->dai_channel_ctl) {
  318. dev_err(rtd->dev,
  319. "%s: dma_data is not set\n",
  320. __func__);
  321. return -EINVAL;
  322. }
  323. if (lab_d->thread_status == MSM_LSM_LAB_THREAD_RUNNING) {
  324. dev_dbg(rtd->dev, "%s: stopping lab thread\n",
  325. __func__);
  326. rc = kthread_stop(session->lsm_lab_thread);
  327. /*
  328. * kthread_stop returns EINTR if the thread_fn
  329. * was not scheduled before calling kthread_stop.
  330. * In this case, we dont need to wait for lab
  331. * thread to complete as lab thread will not be
  332. * scheduled at all.
  333. */
  334. if (rc == -EINTR)
  335. goto done;
  336. /* Wait for the lab thread to exit */
  337. rc = wait_for_completion_timeout(
  338. &lab_d->thread_complete,
  339. MSM_CPE_LAB_THREAD_TIMEOUT);
  340. if (!rc) {
  341. dev_err(rtd->dev,
  342. "%s: Wait for lab thread timedout\n",
  343. __func__);
  344. return -ETIMEDOUT;
  345. }
  346. }
  347. rc = lsm_ops->lab_ch_setup(cpe->core_handle,
  348. session,
  349. WCD_CPE_PRE_DISABLE);
  350. if (rc)
  351. dev_err(rtd->dev,
  352. "%s: PRE ch teardown failed, err = %d\n",
  353. __func__, rc);
  354. /* continue with teardown even if any intermediate step fails */
  355. rc = dma_data->dai_channel_ctl(dma_data, rtd->cpu_dai, false);
  356. if (rc)
  357. dev_err(rtd->dev,
  358. "%s: open data failed %d\n", __func__, rc);
  359. dma_data->ph = 0;
  360. /*
  361. * Even though LAB stop failed,
  362. * output AFE port needs to be stopped
  363. */
  364. rc = afe_ops->afe_port_stop(cpe->core_handle,
  365. &session->afe_out_port_cfg);
  366. if (rc)
  367. dev_err(rtd->dev,
  368. "%s: AFE out port stop failed, err = %d\n",
  369. __func__, rc);
  370. rc = lsm_ops->lab_ch_setup(cpe->core_handle,
  371. session,
  372. WCD_CPE_POST_DISABLE);
  373. if (rc)
  374. dev_err(rtd->dev,
  375. "%s: POST ch teardown failed, err = %d\n",
  376. __func__, rc);
  377. done:
  378. lab_d->thread_status = MSM_LSM_LAB_THREAD_STOP;
  379. lab_d->buf_idx = 0;
  380. atomic_set(&lab_d->in_count, 0);
  381. lab_d->dma_write = 0;
  382. return 0;
  383. }
  384. static int msm_cpe_lab_buf_alloc(struct snd_pcm_substream *substream,
  385. struct cpe_lsm_session *session,
  386. struct msm_slim_dma_data *dma_data)
  387. {
  388. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  389. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  390. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  391. struct cpe_hw_params *hw_params = &lsm_d->hw_params;
  392. struct cpe_data_pcm_buf *pcm_buf = NULL;
  393. int rc = 0;
  394. int dma_alloc = 0;
  395. u32 count = 0;
  396. u32 bufsz, bufcnt;
  397. if (lab_d->pcm_buf &&
  398. lab_d->pcm_buf->mem) {
  399. dev_dbg(rtd->dev,
  400. "%s: LAB buf already allocated\n",
  401. __func__);
  402. goto exit;
  403. }
  404. bufsz = hw_params->buf_sz;
  405. bufcnt = hw_params->period_count;
  406. dev_dbg(rtd->dev,
  407. "%s:Buf Size %d Buf count %d\n",
  408. __func__,
  409. bufsz, bufcnt);
  410. pcm_buf = kzalloc(((sizeof(struct cpe_data_pcm_buf)) * bufcnt),
  411. GFP_KERNEL);
  412. if (!pcm_buf) {
  413. rc = -ENOMEM;
  414. goto exit;
  415. }
  416. lab_d->pcm_buf = pcm_buf;
  417. dma_alloc = bufsz * bufcnt;
  418. pcm_buf->mem = NULL;
  419. pcm_buf->mem = kzalloc(dma_alloc, GFP_DMA);
  420. if (!pcm_buf->mem) {
  421. rc = -ENOMEM;
  422. goto fail;
  423. }
  424. pcm_buf->phys = dma_map_single(dma_data->sdev->dev.parent,
  425. pcm_buf->mem, dma_alloc, DMA_BIDIRECTIONAL);
  426. if (dma_mapping_error(dma_data->sdev->dev.parent, pcm_buf->phys)) {
  427. dev_err(rtd->dev, "%s Error mapping DMA buffers\n", __func__);
  428. pcm_buf->phys = (phys_addr_t)NULL;
  429. rc = -EFAULT;
  430. goto fail;
  431. }
  432. count = 0;
  433. while (count < bufcnt) {
  434. pcm_buf[count].mem = pcm_buf[0].mem + (count * bufsz);
  435. pcm_buf[count].phys = pcm_buf[0].phys + (count * bufsz);
  436. dev_dbg(rtd->dev,
  437. "%s: pcm_buf[%d].mem %pK pcm_buf[%d].phys %pK\n",
  438. __func__, count,
  439. (void *)pcm_buf[count].mem,
  440. count, &(pcm_buf[count].phys));
  441. count++;
  442. }
  443. return 0;
  444. fail:
  445. if (pcm_buf && pcm_buf->mem)
  446. kfree(pcm_buf->mem);
  447. kfree(pcm_buf);
  448. lab_d->pcm_buf = NULL;
  449. exit:
  450. return rc;
  451. }
  452. static int msm_cpe_lab_buf_dealloc(struct snd_pcm_substream *substream,
  453. struct cpe_lsm_session *session, struct msm_slim_dma_data *dma_data)
  454. {
  455. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  456. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  457. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  458. struct cpe_hw_params *hw_params = &lsm_d->hw_params;
  459. int rc = 0;
  460. int dma_alloc = 0;
  461. struct cpe_data_pcm_buf *pcm_buf = NULL;
  462. int bufsz, bufcnt;
  463. bufsz = hw_params->buf_sz;
  464. bufcnt = hw_params->period_count;
  465. dev_dbg(rtd->dev,
  466. "%s:Buf Size %d Buf count %d\n", __func__,
  467. bufsz, bufcnt);
  468. if (bufcnt <= 0 || bufsz <= 0) {
  469. dev_err(rtd->dev,
  470. "%s: Invalid params, bufsz = %u, bufcnt = %u\n",
  471. __func__, bufsz, bufcnt);
  472. return -EINVAL;
  473. }
  474. pcm_buf = lab_d->pcm_buf;
  475. dma_alloc = bufsz * bufcnt;
  476. if (dma_data && pcm_buf)
  477. if (pcm_buf->phys)
  478. dma_unmap_single(dma_data->sdev->dev.parent,
  479. pcm_buf->phys, dma_alloc, DMA_BIDIRECTIONAL);
  480. if (pcm_buf)
  481. kfree(pcm_buf->mem);
  482. kfree(pcm_buf);
  483. lab_d->pcm_buf = NULL;
  484. return rc;
  485. }
  486. /*
  487. * msm_cpe_lab_thread: Initiated on KW detection
  488. * @data: lab data
  489. *
  490. * Start lab thread and call CPE core API for SLIM
  491. * read operations.
  492. */
  493. static int msm_cpe_lab_thread(void *data)
  494. {
  495. struct cpe_lsm_data *lsm_d = data;
  496. struct cpe_lsm_session *session = lsm_d->lsm_session;
  497. struct snd_pcm_substream *substream = lsm_d->substream;
  498. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  499. struct cpe_hw_params *hw_params = &lsm_d->hw_params;
  500. struct cpe_priv *cpe = cpe_get_private_data(substream);
  501. struct wcd_cpe_lsm_ops *lsm_ops;
  502. struct wcd_cpe_afe_ops *afe_ops;
  503. struct cpe_data_pcm_buf *cur_buf, *next_buf;
  504. struct msm_slim_dma_data *dma_data = NULL;
  505. struct snd_soc_pcm_runtime *rtd = NULL;
  506. bool wait_timedout = false;
  507. int rc = 0;
  508. u32 done_len = 0;
  509. u32 buf_count = 0;
  510. u32 prd_cnt;
  511. allow_signal(SIGKILL);
  512. set_current_state(TASK_INTERRUPTIBLE);
  513. pr_debug("%s: Lab thread start\n", __func__);
  514. init_completion(&lab_d->comp);
  515. if (PCM_RUNTIME_CHECK(substream)) {
  516. rc = -EINVAL;
  517. goto done;
  518. }
  519. if (!cpe || !cpe->core_handle) {
  520. pr_err("%s: Handle to %s is invalid\n",
  521. __func__,
  522. (!cpe) ? "cpe" : "core");
  523. rc = -EINVAL;
  524. goto done;
  525. }
  526. rtd = substream->private_data;
  527. if (rtd->cpu_dai)
  528. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
  529. substream);
  530. if (!dma_data || !dma_data->dai_channel_ctl) {
  531. pr_err("%s: dma_data is not set\n", __func__);
  532. rc = -EINVAL;
  533. goto done;
  534. }
  535. lsm_ops = &cpe->lsm_ops;
  536. afe_ops = &cpe->afe_ops;
  537. rc = lsm_ops->lab_ch_setup(cpe->core_handle,
  538. session,
  539. WCD_CPE_PRE_ENABLE);
  540. if (rc) {
  541. dev_err(rtd->dev,
  542. "%s: PRE ch setup failed, err = %d\n",
  543. __func__, rc);
  544. goto done;
  545. }
  546. rc = dma_data->dai_channel_ctl(dma_data, rtd->cpu_dai, true);
  547. if (rc) {
  548. dev_err(rtd->dev,
  549. "%s: open data failed %d\n", __func__, rc);
  550. goto done;
  551. }
  552. dev_dbg(rtd->dev, "%s: Established data channel\n",
  553. __func__);
  554. init_waitqueue_head(&lab_d->period_wait);
  555. memset(lab_d->pcm_buf[0].mem, 0, lab_d->pcm_size);
  556. rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
  557. lab_d->pcm_buf[0].mem,
  558. hw_params->buf_sz, &lab_d->comp);
  559. if (rc) {
  560. dev_err(rtd->dev,
  561. "%s: buf[0] slim_port_xfer failed, err = %d\n",
  562. __func__, rc);
  563. goto done;
  564. }
  565. rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
  566. lab_d->pcm_buf[1].mem,
  567. hw_params->buf_sz, &lab_d->comp);
  568. if (rc) {
  569. dev_err(rtd->dev,
  570. "%s: buf[0] slim_port_xfer failed, err = %d\n",
  571. __func__, rc);
  572. goto done;
  573. }
  574. cur_buf = &lab_d->pcm_buf[0];
  575. next_buf = &lab_d->pcm_buf[2];
  576. prd_cnt = hw_params->period_count;
  577. rc = lsm_ops->lab_ch_setup(cpe->core_handle,
  578. session,
  579. WCD_CPE_POST_ENABLE);
  580. if (rc) {
  581. dev_err(rtd->dev,
  582. "%s: POST ch setup failed, err = %d\n",
  583. __func__, rc);
  584. goto done;
  585. }
  586. rc = afe_ops->afe_port_start(cpe->core_handle,
  587. &session->afe_out_port_cfg);
  588. if (rc) {
  589. dev_err(rtd->dev,
  590. "%s: AFE out port start failed, err = %d\n",
  591. __func__, rc);
  592. goto done;
  593. }
  594. while (!kthread_should_stop() &&
  595. lab_d->thread_status != MSM_LSM_LAB_THREAD_ERROR) {
  596. rc = slim_port_xfer(dma_data->sdev, dma_data->ph,
  597. next_buf->mem,
  598. hw_params->buf_sz, &lab_d->comp);
  599. if (rc) {
  600. dev_err(rtd->dev,
  601. "%s: slim_port_xfer failed, err = %d\n",
  602. __func__, rc);
  603. lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
  604. }
  605. rc = wait_for_completion_timeout(&lab_d->comp, (2 * HZ/10));
  606. if (!rc) {
  607. dev_err(rtd->dev,
  608. "%s: wait timedout for slim buffer\n",
  609. __func__);
  610. wait_timedout = true;
  611. } else {
  612. wait_timedout = false;
  613. }
  614. rc = slim_port_get_xfer_status(dma_data->sdev,
  615. dma_data->ph,
  616. &cur_buf->phys, &done_len);
  617. if (rc ||
  618. (!rc && wait_timedout)) {
  619. dev_err(rtd->dev,
  620. "%s: xfer_status failure, rc = %d, wait_timedout = %s\n",
  621. __func__, rc,
  622. (wait_timedout ? "true" : "false"));
  623. lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
  624. }
  625. if (done_len ||
  626. ((!done_len) &&
  627. lab_d->thread_status == MSM_LSM_LAB_THREAD_ERROR)) {
  628. atomic_inc(&lab_d->in_count);
  629. lab_d->dma_write += snd_pcm_lib_period_bytes(substream);
  630. snd_pcm_period_elapsed(substream);
  631. wake_up(&lab_d->period_wait);
  632. buf_count++;
  633. cur_buf = &lab_d->pcm_buf[buf_count % prd_cnt];
  634. next_buf = &lab_d->pcm_buf[(buf_count + 2) % prd_cnt];
  635. dev_dbg(rtd->dev,
  636. "%s: Cur buf.mem = %pK Next Buf.mem = %pK\n"
  637. " buf count = 0x%x\n", __func__,
  638. cur_buf->mem, next_buf->mem, buf_count);
  639. } else {
  640. dev_err(rtd->dev,
  641. "%s: SB get status, invalid len = 0x%x\n",
  642. __func__, done_len);
  643. }
  644. done_len = 0;
  645. }
  646. done:
  647. if (rc)
  648. lab_d->thread_status = MSM_LSM_LAB_THREAD_ERROR;
  649. pr_debug("%s: Exit lab_thread, exit_status=%d, thread_status=%d\n",
  650. __func__, rc, lab_d->thread_status);
  651. complete(&lab_d->thread_complete);
  652. return 0;
  653. }
  654. /*
  655. * msm_cpe_lsm_open: ASoC call to open the stream
  656. * @substream: substream that is to be opened
  657. *
  658. * Create session data for lsm session and open the lsm session
  659. * on CPE.
  660. */
  661. static int msm_cpe_lsm_open(struct snd_pcm_substream *substream)
  662. {
  663. struct cpe_lsm_data *lsm_d;
  664. struct snd_pcm_runtime *runtime = substream->runtime;
  665. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  666. struct cpe_priv *cpe = cpe_get_private_data(substream);
  667. struct wcd_cpe_lsm_ops *lsm_ops;
  668. int rc = 0;
  669. if (!cpe || !cpe->codec) {
  670. dev_err(rtd->dev,
  671. "%s: Invalid private data\n",
  672. __func__);
  673. return -EINVAL;
  674. }
  675. runtime->hw = msm_pcm_hardware_listen;
  676. rc = snd_pcm_hw_constraint_list(runtime, 0,
  677. SNDRV_PCM_HW_PARAM_RATE,
  678. &constraints_sample_rates);
  679. if (rc < 0) {
  680. pr_err("snd_pcm_hw_constraint_list failed rc %d\n", rc);
  681. return -EINVAL;
  682. }
  683. /* Ensure that buffer size is a multiple of period size */
  684. rc = snd_pcm_hw_constraint_integer(runtime,
  685. SNDRV_PCM_HW_PARAM_PERIODS);
  686. if (rc < 0) {
  687. pr_err("%s: Unable to set pcm_param_periods, rc %d\n",
  688. __func__, rc);
  689. return -EINVAL;
  690. }
  691. rc = snd_pcm_hw_constraint_minmax(runtime,
  692. SNDRV_PCM_HW_PARAM_BUFFER_BYTES,
  693. LISTEN_MIN_NUM_PERIODS * LISTEN_MIN_PERIOD_SIZE,
  694. LISTEN_MAX_NUM_PERIODS * LISTEN_MAX_PERIOD_SIZE);
  695. if (rc < 0) {
  696. pr_err("%s: Unable to set pcm constraints, rc %d\n",
  697. __func__, rc);
  698. return -EINVAL;
  699. }
  700. cpe->core_handle = wcd_cpe_get_core_handle(cpe->codec);
  701. if (!cpe->core_handle) {
  702. dev_err(rtd->dev,
  703. "%s: Invalid handle to codec core\n",
  704. __func__);
  705. return -EINVAL;
  706. }
  707. lsm_ops = &cpe->lsm_ops;
  708. lsm_d = kzalloc(sizeof(struct cpe_lsm_data), GFP_KERNEL);
  709. if (!lsm_d) {
  710. dev_err(rtd->dev,
  711. "%s: ENOMEM for lsm session, size = %zd\n",
  712. __func__, sizeof(struct cpe_lsm_data));
  713. rc = -ENOMEM;
  714. goto fail_return;
  715. }
  716. mutex_init(&lsm_d->lsm_api_lock);
  717. lsm_d->lsm_session = lsm_ops->lsm_alloc_session(cpe->core_handle,
  718. lsm_d, msm_cpe_process_event_status);
  719. if (!lsm_d->lsm_session) {
  720. dev_err(rtd->dev,
  721. "%s: session allocation failed",
  722. __func__);
  723. rc = -EINVAL;
  724. goto fail_session_alloc;
  725. }
  726. /* Explicitly Assign the LAB thread to STOP state */
  727. lsm_d->lab.thread_status = MSM_LSM_LAB_THREAD_STOP;
  728. lsm_d->lsm_session->started = false;
  729. lsm_d->substream = substream;
  730. init_waitqueue_head(&lsm_d->lab.period_wait);
  731. lsm_d->cpe_prepared = false;
  732. dev_dbg(rtd->dev, "%s: allocated session with id = %d\n",
  733. __func__, lsm_d->lsm_session->id);
  734. rc = lsm_ops->lsm_open_tx(cpe->core_handle, lsm_d->lsm_session,
  735. LSM_VOICE_WAKEUP_APP_V2, 16000);
  736. if (rc < 0) {
  737. dev_err(rtd->dev,
  738. "%s: OPEN_TX cmd failed, err = %d\n",
  739. __func__, rc);
  740. goto fail_open_tx;
  741. }
  742. init_waitqueue_head(&lsm_d->event_wait);
  743. atomic_set(&lsm_d->event_avail, 0);
  744. atomic_set(&lsm_d->event_stop, 0);
  745. runtime->private_data = lsm_d;
  746. return 0;
  747. fail_open_tx:
  748. lsm_ops->lsm_dealloc_session(cpe->core_handle, lsm_d->lsm_session);
  749. fail_session_alloc:
  750. mutex_destroy(&lsm_d->lsm_api_lock);
  751. kfree(lsm_d);
  752. fail_return:
  753. return rc;
  754. }
  755. /*
  756. * msm_cpe_lsm_close: ASoC call to close/cleanup the stream
  757. * @substream: substream that is to be closed
  758. *
  759. * Deallocate the session and release the AFE port. It is not
  760. * required to deregister the sound model as long as we close
  761. * the lsm session on CPE.
  762. */
  763. static int msm_cpe_lsm_close(struct snd_pcm_substream *substream)
  764. {
  765. struct snd_pcm_runtime *runtime = substream->runtime;
  766. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  767. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  768. struct cpe_priv *cpe = cpe_get_private_data(substream);
  769. struct wcd_cpe_lsm_ops *lsm_ops;
  770. struct cpe_lsm_session *session;
  771. struct wcd_cpe_afe_ops *afe_ops;
  772. struct wcd_cpe_afe_port_cfg *afe_cfg;
  773. int rc = 0;
  774. if (!cpe || !cpe->core_handle) {
  775. dev_err(rtd->dev,
  776. "%s: Invalid private data\n",
  777. __func__);
  778. return -EINVAL;
  779. }
  780. if (!lsm_d || !lsm_d->lsm_session) {
  781. dev_err(rtd->dev,
  782. "%s: Invalid session data\n",
  783. __func__);
  784. return -EINVAL;
  785. }
  786. lsm_ops = &cpe->lsm_ops;
  787. session = lsm_d->lsm_session;
  788. afe_ops = &cpe->afe_ops;
  789. afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
  790. /*
  791. * If driver is closed without stopping LAB,
  792. * explicitly stop LAB before cleaning up the
  793. * driver resources.
  794. */
  795. rc = msm_cpe_lsm_lab_stop(substream);
  796. if (rc) {
  797. dev_err(rtd->dev,
  798. "%s: Failed to stop lab, error = %d\n",
  799. __func__, rc);
  800. return rc;
  801. }
  802. rc = msm_cpe_afe_port_cntl(substream,
  803. cpe->core_handle,
  804. afe_ops, afe_cfg,
  805. AFE_CMD_PORT_STOP);
  806. lsm_d->cpe_prepared = false;
  807. rc = lsm_ops->lsm_close_tx(cpe->core_handle, session);
  808. if (rc != 0) {
  809. dev_err(rtd->dev,
  810. "%s: lsm_close fail, err = %d\n",
  811. __func__, rc);
  812. return rc;
  813. }
  814. lsm_ops->lsm_dealloc_session(cpe->core_handle, session);
  815. runtime->private_data = NULL;
  816. mutex_destroy(&lsm_d->lsm_api_lock);
  817. kfree(lsm_d);
  818. return rc;
  819. }
  820. static int msm_cpe_lsm_get_conf_levels(
  821. struct cpe_lsm_session *session,
  822. u8 *conf_levels_ptr)
  823. {
  824. int rc = 0;
  825. if (session->num_confidence_levels <= 0) {
  826. pr_debug("%s: conf_levels (%u), skip set params\n",
  827. __func__,
  828. session->num_confidence_levels);
  829. goto done;
  830. }
  831. session->conf_levels = kzalloc(session->num_confidence_levels,
  832. GFP_KERNEL);
  833. if (!session->conf_levels) {
  834. rc = -ENOMEM;
  835. goto done;
  836. }
  837. if (copy_from_user(session->conf_levels,
  838. conf_levels_ptr,
  839. session->num_confidence_levels)) {
  840. pr_err("%s: copy_from_user failed for confidence levels %u\n",
  841. __func__, session->num_confidence_levels);
  842. kfree(session->conf_levels);
  843. session->conf_levels = NULL;
  844. rc = -EFAULT;
  845. goto done;
  846. }
  847. done:
  848. return rc;
  849. }
  850. static int msm_cpe_lsm_validate_out_format(
  851. struct snd_pcm_substream *substream,
  852. struct snd_lsm_output_format_cfg *cfg)
  853. {
  854. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  855. int rc = 0;
  856. if (!cfg) {
  857. dev_err(rtd->dev,
  858. "%s: Invalid lsm out cfg\n", __func__);
  859. rc = -EINVAL;
  860. goto done;
  861. }
  862. if (cfg->format != LSM_OUT_FORMAT_PCM &&
  863. cfg->format != LSM_OUT_FORMAT_ADPCM) {
  864. dev_err(rtd->dev,
  865. "%s: Invalid format %u\n",
  866. __func__, cfg->format);
  867. rc = -EINVAL;
  868. goto done;
  869. }
  870. if (cfg->packing != LSM_OUT_DATA_RAW &&
  871. cfg->packing != LSM_OUT_DATA_PACKED) {
  872. dev_err(rtd->dev,
  873. "%s: Invalid packing method %u\n",
  874. __func__, cfg->packing);
  875. rc = -EINVAL;
  876. goto done;
  877. }
  878. if (cfg->events != LSM_OUT_DATA_EVENTS_DISABLED &&
  879. cfg->events != LSM_OUT_DATA_EVENTS_ENABLED) {
  880. dev_err(rtd->dev,
  881. "%s: Invalid events provided %u\n",
  882. __func__, cfg->events);
  883. rc = -EINVAL;
  884. goto done;
  885. }
  886. if (cfg->mode != LSM_OUT_TRANSFER_MODE_RT &&
  887. cfg->mode != LSM_OUT_TRANSFER_MODE_FTRT) {
  888. dev_err(rtd->dev,
  889. "%s: Invalid transfer mode %u\n",
  890. __func__, cfg->mode);
  891. rc = -EINVAL;
  892. goto done;
  893. }
  894. done:
  895. return rc;
  896. }
  897. /*
  898. * msm_cpe_lsm_ioctl_shared: Shared IOCTL for this platform driver
  899. * @substream: ASoC substream for which the operation is invoked
  900. * @cmd: command for the ioctl
  901. * @arg: argument for the ioctl
  902. *
  903. * Perform dedicated listen functions like register sound model,
  904. * deregister sound model, etc
  905. * Called with lsm_api_lock acquired.
  906. */
  907. static int msm_cpe_lsm_ioctl_shared(struct snd_pcm_substream *substream,
  908. unsigned int cmd, void *arg)
  909. {
  910. struct snd_lsm_sound_model_v2 snd_model;
  911. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  912. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  913. struct cpe_priv *cpe = cpe_get_private_data(substream);
  914. struct cpe_lsm_session *session;
  915. struct wcd_cpe_lsm_ops *lsm_ops;
  916. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  917. struct snd_dma_buffer *dma_buf = &substream->dma_buffer;
  918. struct msm_slim_dma_data *dma_data = NULL;
  919. struct snd_lsm_detection_params det_params;
  920. int rc = 0;
  921. if (!cpe || !cpe->core_handle) {
  922. dev_err(rtd->dev,
  923. "%s: Invalid private data\n",
  924. __func__);
  925. return -EINVAL;
  926. }
  927. if (!lsm_d || !lsm_d->lsm_session) {
  928. dev_err(rtd->dev,
  929. "%s: Invalid session data\n",
  930. __func__);
  931. return -EINVAL;
  932. }
  933. session = lsm_d->lsm_session;
  934. lsm_ops = &cpe->lsm_ops;
  935. switch (cmd) {
  936. case SNDRV_LSM_STOP_LAB:
  937. dev_dbg(rtd->dev,
  938. "%s: %s, lab_enable = %d, lab_thread_ststus = %d\n",
  939. __func__, "SNDRV_LSM_STOP_LAB",
  940. session->lab_enable,
  941. lab_d->thread_status);
  942. if (session->lab_enable &&
  943. lab_d->thread_status != MSM_LSM_LAB_THREAD_STOP) {
  944. atomic_inc(&lab_d->abort_read);
  945. wake_up(&lab_d->period_wait);
  946. rc = msm_cpe_lsm_lab_stop(substream);
  947. if (rc) {
  948. dev_err(rtd->dev,
  949. "%s: stop LAB failed, error = %d\n",
  950. __func__, rc);
  951. return rc;
  952. }
  953. } else if (!session->lab_enable) {
  954. dev_dbg(rtd->dev,
  955. "%s: LAB already stopped\n",
  956. __func__);
  957. }
  958. break;
  959. case SNDRV_LSM_LAB_CONTROL:
  960. if (copy_from_user(&session->lab_enable, (void *)arg,
  961. sizeof(u32))) {
  962. dev_err(rtd->dev,
  963. "%s: copy_from_user failed, size %zd\n",
  964. __func__, sizeof(u32));
  965. return -EFAULT;
  966. }
  967. dev_dbg(rtd->dev,
  968. "%s: %s, lab_enable = %d\n",
  969. __func__, "SNDRV_LSM_LAB_CONTROL",
  970. session->lab_enable);
  971. if (rtd->cpu_dai)
  972. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
  973. substream);
  974. if (!dma_data || !dma_data->dai_channel_ctl) {
  975. dev_err(rtd->dev,
  976. "%s: dma_data is not set\n", __func__);
  977. return -EINVAL;
  978. }
  979. if (session->lab_enable) {
  980. rc = msm_cpe_lab_buf_alloc(substream,
  981. session, dma_data);
  982. if (rc < 0) {
  983. dev_err(rtd->dev,
  984. "%s: lab buffer alloc failed, err = %d\n",
  985. __func__, rc);
  986. return rc;
  987. }
  988. dma_buf->dev.type = SNDRV_DMA_TYPE_DEV;
  989. dma_buf->dev.dev = substream->pcm->card->dev;
  990. dma_buf->private_data = NULL;
  991. dma_buf->area = lab_d->pcm_buf[0].mem;
  992. dma_buf->addr = lab_d->pcm_buf[0].phys;
  993. dma_buf->bytes = (lsm_d->hw_params.buf_sz *
  994. lsm_d->hw_params.period_count);
  995. init_completion(&lab_d->thread_complete);
  996. snd_pcm_set_runtime_buffer(substream,
  997. &substream->dma_buffer);
  998. rc = lsm_ops->lsm_lab_control(cpe->core_handle,
  999. session, true);
  1000. if (rc < 0) {
  1001. dev_err(rtd->dev,
  1002. "%s: Lab Enable Failed rc %d\n",
  1003. __func__, rc);
  1004. return rc;
  1005. }
  1006. } else {
  1007. /*
  1008. * It is possible that lab is still enabled
  1009. * when trying to de-allocate the lab buffer.
  1010. * Make sure to disable lab before de-allocating
  1011. * the lab buffer.
  1012. */
  1013. rc = msm_cpe_lsm_lab_stop(substream);
  1014. if (rc < 0) {
  1015. dev_err(rtd->dev,
  1016. "%s: LAB stop failed, error = %d\n",
  1017. __func__, rc);
  1018. return rc;
  1019. }
  1020. /*
  1021. * Buffer has to be de-allocated even if
  1022. * lab_control failed.
  1023. */
  1024. rc = msm_cpe_lab_buf_dealloc(substream,
  1025. session, dma_data);
  1026. if (rc < 0) {
  1027. dev_err(rtd->dev,
  1028. "%s: lab buffer free failed, err = %d\n",
  1029. __func__, rc);
  1030. return rc;
  1031. }
  1032. }
  1033. break;
  1034. case SNDRV_LSM_REG_SND_MODEL_V2:
  1035. dev_dbg(rtd->dev,
  1036. "%s: %s\n",
  1037. __func__, "SNDRV_LSM_REG_SND_MODEL_V2");
  1038. memcpy(&snd_model, arg,
  1039. sizeof(struct snd_lsm_sound_model_v2));
  1040. session->num_confidence_levels =
  1041. snd_model.num_confidence_levels;
  1042. rc = msm_cpe_lsm_get_conf_levels(session,
  1043. snd_model.confidence_level);
  1044. if (rc) {
  1045. dev_err(rtd->dev,
  1046. "%s: %s get_conf_levels fail, err = %d\n",
  1047. __func__, "SNDRV_LSM_REG_SND_MODEL_V2",
  1048. rc);
  1049. break;
  1050. }
  1051. session->snd_model_data = kzalloc(snd_model.data_size,
  1052. GFP_KERNEL);
  1053. if (!session->snd_model_data) {
  1054. kfree(session->conf_levels);
  1055. session->conf_levels = NULL;
  1056. return -ENOMEM;
  1057. }
  1058. session->snd_model_size = snd_model.data_size;
  1059. if (copy_from_user(session->snd_model_data,
  1060. snd_model.data, snd_model.data_size)) {
  1061. dev_err(rtd->dev,
  1062. "%s: copy_from_user failed for snd_model\n",
  1063. __func__);
  1064. kfree(session->conf_levels);
  1065. kfree(session->snd_model_data);
  1066. session->conf_levels = NULL;
  1067. session->snd_model_data = NULL;
  1068. return -EFAULT;
  1069. }
  1070. rc = lsm_ops->lsm_shmem_alloc(cpe->core_handle, session,
  1071. session->snd_model_size);
  1072. if (rc != 0) {
  1073. dev_err(rtd->dev,
  1074. "%s: shared memory allocation failed, err = %d\n",
  1075. __func__, rc);
  1076. kfree(session->snd_model_data);
  1077. kfree(session->conf_levels);
  1078. session->snd_model_data = NULL;
  1079. session->conf_levels = NULL;
  1080. return rc;
  1081. }
  1082. rc = lsm_ops->lsm_register_snd_model(cpe->core_handle, session,
  1083. snd_model.detection_mode,
  1084. snd_model.detect_failure);
  1085. if (rc != 0) {
  1086. dev_err(rtd->dev,
  1087. "%s: snd_model_reg failed, err = %d\n",
  1088. __func__, rc);
  1089. lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
  1090. kfree(session->snd_model_data);
  1091. kfree(session->conf_levels);
  1092. session->snd_model_data = NULL;
  1093. session->conf_levels = NULL;
  1094. return rc;
  1095. }
  1096. break;
  1097. case SNDRV_LSM_DEREG_SND_MODEL:
  1098. dev_dbg(rtd->dev,
  1099. "%s: %s\n",
  1100. __func__, "SNDRV_LSM_DEREG_SND_MODEL");
  1101. if (session->lab_enable) {
  1102. /*
  1103. * It is possible that lab is still enabled
  1104. * when trying to deregister sound model.
  1105. * Make sure to disable lab before de-allocating
  1106. * the lab buffer.
  1107. */
  1108. rc = msm_cpe_lsm_lab_stop(substream);
  1109. if (rc) {
  1110. dev_err(rtd->dev,
  1111. "%s: LAB stop failed, error = %d\n",
  1112. __func__, rc);
  1113. return rc;
  1114. }
  1115. rc = lsm_ops->lsm_lab_control(cpe->core_handle,
  1116. session, false);
  1117. if (rc)
  1118. dev_err(rtd->dev,
  1119. "%s: Lab Disable Failed rc %d\n",
  1120. __func__, rc);
  1121. dma_data = snd_soc_dai_get_dma_data(rtd->cpu_dai,
  1122. substream);
  1123. if (!dma_data || !dma_data->dai_channel_ctl)
  1124. dev_err(rtd->dev,
  1125. "%s: dma_data is not set\n", __func__);
  1126. /*
  1127. * Buffer has to be de-allocated even if
  1128. * lab_control failed and/or dma data is invalid.
  1129. */
  1130. rc = msm_cpe_lab_buf_dealloc(substream,
  1131. session, dma_data);
  1132. if (rc < 0)
  1133. dev_err(rtd->dev,
  1134. "%s: lab buffer free failed, err = %d\n",
  1135. __func__, rc);
  1136. }
  1137. rc = lsm_ops->lsm_deregister_snd_model(
  1138. cpe->core_handle, session);
  1139. if (rc != 0) {
  1140. dev_err(rtd->dev,
  1141. "%s: snd_model de-reg failed, err = %d\n",
  1142. __func__, rc);
  1143. return rc;
  1144. }
  1145. kfree(session->snd_model_data);
  1146. kfree(session->conf_levels);
  1147. session->snd_model_data = NULL;
  1148. session->conf_levels = NULL;
  1149. rc = lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
  1150. if (rc != 0) {
  1151. dev_err(rtd->dev,
  1152. "%s: LSM shared memory dealloc failed, err = %d\n",
  1153. __func__, rc);
  1154. return rc;
  1155. }
  1156. break;
  1157. case SNDRV_LSM_EVENT_STATUS:
  1158. case SNDRV_LSM_EVENT_STATUS_V3: {
  1159. struct snd_lsm_event_status *user;
  1160. struct snd_lsm_event_status_v3 *user_v3;
  1161. dev_dbg(rtd->dev,
  1162. "%s: %s\n",
  1163. __func__, "SNDRV_LSM_EVENT_STATUS(_V3)");
  1164. if (!arg) {
  1165. dev_err(rtd->dev,
  1166. "%s: Invalid argument to ioctl %s\n",
  1167. __func__,
  1168. "SNDRV_LSM_EVENT_STATUS(_V3)");
  1169. return -EINVAL;
  1170. }
  1171. /*
  1172. * Release the api lock before wait to allow
  1173. * other IOCTLs to be invoked while waiting
  1174. * for event
  1175. */
  1176. MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
  1177. "lsm_api_lock");
  1178. rc = wait_event_freezable(lsm_d->event_wait,
  1179. (atomic_read(&lsm_d->event_avail) == 1) ||
  1180. (atomic_read(&lsm_d->event_stop) == 1));
  1181. MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
  1182. "lsm_api_lock");
  1183. if (!rc) {
  1184. if (atomic_read(&lsm_d->event_avail) == 1) {
  1185. rc = 0;
  1186. atomic_set(&lsm_d->event_avail, 0);
  1187. if (cmd == SNDRV_LSM_EVENT_STATUS) {
  1188. user = arg;
  1189. if (lsm_d->ev_det_pld_size >
  1190. user->payload_size) {
  1191. dev_err(rtd->dev,
  1192. "%s: avail pld_bytes = %u, needed = %u\n",
  1193. __func__,
  1194. user->payload_size,
  1195. lsm_d->ev_det_pld_size);
  1196. return -EINVAL;
  1197. }
  1198. user->status = lsm_d->ev_det_status;
  1199. user->payload_size =
  1200. lsm_d->ev_det_pld_size;
  1201. memcpy(user->payload,
  1202. lsm_d->ev_det_payload,
  1203. lsm_d->ev_det_pld_size);
  1204. } else {
  1205. user_v3 = arg;
  1206. if (lsm_d->ev_det_pld_size >
  1207. user_v3->payload_size) {
  1208. dev_err(rtd->dev,
  1209. "%s: avail pld_bytes = %u, needed = %u\n",
  1210. __func__,
  1211. user_v3->payload_size,
  1212. lsm_d->ev_det_pld_size);
  1213. return -EINVAL;
  1214. }
  1215. /* event status timestamp not supported
  1216. * on CPE mode. Set msw and lsw to 0.
  1217. */
  1218. user_v3->timestamp_lsw = 0;
  1219. user_v3->timestamp_msw = 0;
  1220. user_v3->status = lsm_d->ev_det_status;
  1221. user_v3->payload_size =
  1222. lsm_d->ev_det_pld_size;
  1223. memcpy(user_v3->payload,
  1224. lsm_d->ev_det_payload,
  1225. lsm_d->ev_det_pld_size);
  1226. }
  1227. } else if (atomic_read(&lsm_d->event_stop) == 1) {
  1228. dev_dbg(rtd->dev,
  1229. "%s: wait_aborted\n", __func__);
  1230. if (cmd == SNDRV_LSM_EVENT_STATUS) {
  1231. user = arg;
  1232. user->payload_size = 0;
  1233. } else {
  1234. user_v3 = arg;
  1235. user_v3->payload_size = 0;
  1236. }
  1237. rc = 0;
  1238. }
  1239. }
  1240. }
  1241. break;
  1242. case SNDRV_LSM_ABORT_EVENT:
  1243. dev_dbg(rtd->dev,
  1244. "%s: %s\n",
  1245. __func__, "SNDRV_LSM_ABORT_EVENT");
  1246. atomic_set(&lsm_d->event_stop, 1);
  1247. wake_up(&lsm_d->event_wait);
  1248. break;
  1249. case SNDRV_LSM_START:
  1250. dev_dbg(rtd->dev,
  1251. "%s: %s\n",
  1252. __func__, "SNDRV_LSM_START");
  1253. rc = lsm_ops->lsm_start(cpe->core_handle, session);
  1254. if (rc != 0) {
  1255. dev_err(rtd->dev,
  1256. "%s: lsm_start fail, err = %d\n",
  1257. __func__, rc);
  1258. return rc;
  1259. }
  1260. session->started = true;
  1261. break;
  1262. case SNDRV_LSM_STOP:
  1263. dev_dbg(rtd->dev,
  1264. "%s: %s, lab_enable = %d, lab_thread_status = %d\n",
  1265. __func__, "SNDRV_LSM_STOP",
  1266. session->lab_enable,
  1267. lab_d->thread_status);
  1268. if ((session->lab_enable &&
  1269. lab_d->thread_status ==
  1270. MSM_LSM_LAB_THREAD_RUNNING)) {
  1271. /* Explicitly stop LAB */
  1272. rc = msm_cpe_lsm_lab_stop(substream);
  1273. if (rc) {
  1274. dev_err(rtd->dev,
  1275. "%s: lab_stop failed, err = %d\n",
  1276. __func__, rc);
  1277. return rc;
  1278. }
  1279. }
  1280. rc = lsm_ops->lsm_stop(cpe->core_handle, session);
  1281. if (rc != 0) {
  1282. dev_err(rtd->dev,
  1283. "%s: lsm_stop fail err = %d\n",
  1284. __func__, rc);
  1285. return rc;
  1286. }
  1287. session->started = false;
  1288. break;
  1289. case SNDRV_LSM_SET_PARAMS:
  1290. memcpy(&det_params, arg,
  1291. sizeof(det_params));
  1292. if (det_params.num_confidence_levels <= 0) {
  1293. dev_err(rtd->dev,
  1294. "%s: %s: Invalid confidence levels %u\n",
  1295. __func__, "SNDRV_LSM_SET_PARAMS",
  1296. det_params.num_confidence_levels);
  1297. return -EINVAL;
  1298. }
  1299. session->num_confidence_levels =
  1300. det_params.num_confidence_levels;
  1301. rc = msm_cpe_lsm_get_conf_levels(session,
  1302. det_params.conf_level);
  1303. if (rc) {
  1304. dev_err(rtd->dev,
  1305. "%s: %s get_conf_levels fail, err = %d\n",
  1306. __func__, "SNDRV_LSM_SET_PARAMS",
  1307. rc);
  1308. break;
  1309. }
  1310. rc = lsm_ops->lsm_set_data(cpe->core_handle, session,
  1311. det_params.detect_mode,
  1312. det_params.detect_failure);
  1313. if (rc) {
  1314. dev_err(rtd->dev,
  1315. "%s: lsm_set_data failed, err = %d\n",
  1316. __func__, rc);
  1317. return rc;
  1318. }
  1319. kfree(session->conf_levels);
  1320. session->conf_levels = NULL;
  1321. break;
  1322. case SNDRV_LSM_OUT_FORMAT_CFG: {
  1323. struct snd_lsm_output_format_cfg u_fmt_cfg;
  1324. if (!arg) {
  1325. dev_err(rtd->dev,
  1326. "%s: Invalid argument to ioctl %s\n",
  1327. __func__, "SNDRV_LSM_OUT_FORMAT_CFG");
  1328. return -EINVAL;
  1329. }
  1330. if (copy_from_user(&u_fmt_cfg, arg,
  1331. sizeof(u_fmt_cfg))) {
  1332. dev_err(rtd->dev,
  1333. "%s: copy_from_user failed for out_fmt_cfg\n",
  1334. __func__);
  1335. return -EFAULT;
  1336. }
  1337. if (msm_cpe_lsm_validate_out_format(substream,
  1338. &u_fmt_cfg))
  1339. return -EINVAL;
  1340. session->out_fmt_cfg.format = u_fmt_cfg.format;
  1341. session->out_fmt_cfg.pack_mode = u_fmt_cfg.packing;
  1342. session->out_fmt_cfg.data_path_events =
  1343. u_fmt_cfg.events;
  1344. session->out_fmt_cfg.transfer_mode = u_fmt_cfg.mode;
  1345. rc = lsm_ops->lsm_set_fmt_cfg(cpe->core_handle,
  1346. session);
  1347. if (rc) {
  1348. dev_err(rtd->dev,
  1349. "%s: lsm_set_fmt_cfg failed, err = %d\n",
  1350. __func__, rc);
  1351. return rc;
  1352. }
  1353. }
  1354. break;
  1355. case SNDRV_LSM_SET_PORT: {
  1356. u32 port_id = cpe->input_port_id;
  1357. dev_dbg(rtd->dev, "%s: %s\n", __func__, "SNDRV_LSM_SET_PORT");
  1358. rc = lsm_ops->lsm_set_port(cpe->core_handle, session, &port_id);
  1359. if (rc) {
  1360. dev_err(rtd->dev,
  1361. "%s: lsm_set_port failed, err = %d\n",
  1362. __func__, rc);
  1363. return rc;
  1364. }
  1365. }
  1366. break;
  1367. default:
  1368. dev_dbg(rtd->dev,
  1369. "%s: Default snd_lib_ioctl cmd 0x%x\n",
  1370. __func__, cmd);
  1371. rc = snd_pcm_lib_ioctl(substream, cmd, arg);
  1372. }
  1373. return rc;
  1374. }
  1375. static int msm_cpe_lsm_lab_start(struct snd_pcm_substream *substream,
  1376. u16 event_det_status)
  1377. {
  1378. struct snd_soc_pcm_runtime *rtd;
  1379. struct cpe_lsm_data *lsm_d = NULL;
  1380. struct cpe_priv *cpe = NULL;
  1381. struct cpe_lsm_session *session = NULL;
  1382. struct cpe_lsm_lab *lab_d = NULL;
  1383. struct cpe_hw_params *hw_params;
  1384. struct wcd_cpe_lsm_ops *lsm_ops;
  1385. struct wcd_cpe_afe_ops *afe_ops;
  1386. struct wcd_cpe_afe_port_cfg *out_port;
  1387. int rc;
  1388. if (!substream || !substream->private_data) {
  1389. pr_err("%s: invalid substream (%pK)\n",
  1390. __func__, substream);
  1391. return -EINVAL;
  1392. }
  1393. rtd = substream->private_data;
  1394. lsm_d = cpe_get_lsm_data(substream);
  1395. cpe = cpe_get_private_data(substream);
  1396. if (!cpe || !cpe->core_handle) {
  1397. dev_err(rtd->dev,
  1398. "%s: Invalid private data\n",
  1399. __func__);
  1400. return -EINVAL;
  1401. }
  1402. if (!lsm_d || !lsm_d->lsm_session) {
  1403. dev_err(rtd->dev,
  1404. "%s: Invalid session data\n",
  1405. __func__);
  1406. return -EINVAL;
  1407. }
  1408. session = lsm_d->lsm_session;
  1409. lsm_ops = &cpe->lsm_ops;
  1410. lab_d = &lsm_d->lab;
  1411. afe_ops = &cpe->afe_ops;
  1412. hw_params = &lsm_d->hw_params;
  1413. if (!session->started) {
  1414. dev_dbg(rtd->dev,
  1415. "%s: Session is stopped, cannot start LAB\n",
  1416. __func__);
  1417. return 0;
  1418. }
  1419. reinit_completion(&lab_d->thread_complete);
  1420. if (session->lab_enable &&
  1421. event_det_status ==
  1422. LSM_VOICE_WAKEUP_STATUS_DETECTED) {
  1423. out_port = &session->afe_out_port_cfg;
  1424. out_port->port_id = session->afe_out_port_id;
  1425. out_port->bit_width = hw_params->sample_size;
  1426. out_port->num_channels = hw_params->channels;
  1427. out_port->sample_rate = hw_params->sample_rate;
  1428. dev_dbg(rtd->dev, "%s: port_id= %u, bit_width= %u, rate= %u\n",
  1429. __func__, out_port->port_id, out_port->bit_width,
  1430. out_port->sample_rate);
  1431. rc = afe_ops->afe_port_cmd_cfg(cpe->core_handle,
  1432. out_port);
  1433. if (rc) {
  1434. dev_err(rtd->dev,
  1435. "%s: Failed afe generic config v2, err = %d\n",
  1436. __func__, rc);
  1437. return rc;
  1438. }
  1439. atomic_set(&lab_d->abort_read, 0);
  1440. dev_dbg(rtd->dev,
  1441. "%s: KW detected, scheduling LAB thread\n",
  1442. __func__);
  1443. /*
  1444. * Even though thread might be only scheduled and
  1445. * not currently running, mark the internal driver
  1446. * status to running so driver can cancel this thread
  1447. * if it needs to before the thread gets chance to run.
  1448. */
  1449. lab_d->thread_status = MSM_LSM_LAB_THREAD_RUNNING;
  1450. session->lsm_lab_thread = kthread_run(
  1451. msm_cpe_lab_thread,
  1452. lsm_d,
  1453. "lab_thread");
  1454. }
  1455. return 0;
  1456. }
  1457. static bool msm_cpe_lsm_is_valid_stream(struct snd_pcm_substream *substream,
  1458. const char *func)
  1459. {
  1460. struct snd_soc_pcm_runtime *rtd;
  1461. struct cpe_lsm_data *lsm_d = NULL;
  1462. struct cpe_priv *cpe = NULL;
  1463. struct cpe_lsm_session *session = NULL;
  1464. struct wcd_cpe_lsm_ops *lsm_ops;
  1465. if (!substream || !substream->private_data) {
  1466. pr_err("%s: invalid substream (%pK)\n",
  1467. func, substream);
  1468. return false;
  1469. }
  1470. rtd = substream->private_data;
  1471. lsm_d = cpe_get_lsm_data(substream);
  1472. cpe = cpe_get_private_data(substream);
  1473. if (!cpe || !cpe->core_handle) {
  1474. dev_err(rtd->dev,
  1475. "%s: Invalid private data\n",
  1476. func);
  1477. return false;
  1478. }
  1479. if (!lsm_d || !lsm_d->lsm_session) {
  1480. dev_err(rtd->dev,
  1481. "%s: Invalid session data\n",
  1482. func);
  1483. return false;
  1484. }
  1485. session = lsm_d->lsm_session;
  1486. lsm_ops = &cpe->lsm_ops;
  1487. if (!lsm_ops) {
  1488. dev_err(rtd->dev,
  1489. "%s: Invalid lsm_ops\n", func);
  1490. return false;
  1491. }
  1492. return true;
  1493. }
  1494. static int msm_cpe_lsm_set_epd(struct snd_pcm_substream *substream,
  1495. struct lsm_params_info *p_info)
  1496. {
  1497. struct snd_soc_pcm_runtime *rtd;
  1498. struct cpe_lsm_data *lsm_d = NULL;
  1499. struct cpe_priv *cpe = NULL;
  1500. struct cpe_lsm_session *session = NULL;
  1501. struct wcd_cpe_lsm_ops *lsm_ops;
  1502. struct snd_lsm_ep_det_thres epd_thres;
  1503. int rc;
  1504. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1505. return -EINVAL;
  1506. rtd = substream->private_data;
  1507. lsm_d = cpe_get_lsm_data(substream);
  1508. cpe = cpe_get_private_data(substream);
  1509. session = lsm_d->lsm_session;
  1510. lsm_ops = &cpe->lsm_ops;
  1511. if (p_info->param_size != sizeof(epd_thres)) {
  1512. dev_err(rtd->dev,
  1513. "%s: Invalid param_size %d\n",
  1514. __func__, p_info->param_size);
  1515. rc = -EINVAL;
  1516. goto done;
  1517. }
  1518. if (copy_from_user(&epd_thres, p_info->param_data,
  1519. p_info->param_size)) {
  1520. dev_err(rtd->dev,
  1521. "%s: copy_from_user failed, size = %d\n",
  1522. __func__, p_info->param_size);
  1523. rc = -EFAULT;
  1524. goto done;
  1525. }
  1526. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1527. session, p_info, &epd_thres,
  1528. LSM_ENDPOINT_DETECT_THRESHOLD);
  1529. if (unlikely(rc))
  1530. dev_err(rtd->dev,
  1531. "%s: set_one_param(epd_threshold) failed, rc %d\n",
  1532. __func__, rc);
  1533. done:
  1534. return rc;
  1535. }
  1536. static int msm_cpe_lsm_set_mode(struct snd_pcm_substream *substream,
  1537. struct lsm_params_info *p_info)
  1538. {
  1539. struct snd_soc_pcm_runtime *rtd;
  1540. struct cpe_lsm_data *lsm_d = NULL;
  1541. struct cpe_priv *cpe = NULL;
  1542. struct cpe_lsm_session *session = NULL;
  1543. struct wcd_cpe_lsm_ops *lsm_ops;
  1544. struct snd_lsm_detect_mode det_mode;
  1545. int rc;
  1546. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1547. return -EINVAL;
  1548. rtd = substream->private_data;
  1549. lsm_d = cpe_get_lsm_data(substream);
  1550. cpe = cpe_get_private_data(substream);
  1551. session = lsm_d->lsm_session;
  1552. lsm_ops = &cpe->lsm_ops;
  1553. if (p_info->param_size != sizeof(det_mode)) {
  1554. dev_err(rtd->dev,
  1555. "%s: Invalid param_size %d\n",
  1556. __func__, p_info->param_size);
  1557. rc = -EINVAL;
  1558. goto done;
  1559. }
  1560. if (copy_from_user(&det_mode, p_info->param_data,
  1561. p_info->param_size)) {
  1562. dev_err(rtd->dev,
  1563. "%s: copy_from_user failed, size = %d\n",
  1564. __func__, p_info->param_size);
  1565. rc = -EFAULT;
  1566. goto done;
  1567. }
  1568. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1569. session, p_info, &det_mode,
  1570. LSM_OPERATION_MODE);
  1571. if (unlikely(rc))
  1572. dev_err(rtd->dev,
  1573. "%s: set_one_param(epd_threshold) failed, rc %d\n",
  1574. __func__, rc);
  1575. done:
  1576. return rc;
  1577. }
  1578. static int msm_cpe_lsm_set_gain(struct snd_pcm_substream *substream,
  1579. struct lsm_params_info *p_info)
  1580. {
  1581. struct snd_soc_pcm_runtime *rtd;
  1582. struct cpe_lsm_data *lsm_d = NULL;
  1583. struct cpe_priv *cpe = NULL;
  1584. struct cpe_lsm_session *session = NULL;
  1585. struct wcd_cpe_lsm_ops *lsm_ops;
  1586. struct snd_lsm_gain gain;
  1587. int rc;
  1588. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1589. return -EINVAL;
  1590. rtd = substream->private_data;
  1591. lsm_d = cpe_get_lsm_data(substream);
  1592. cpe = cpe_get_private_data(substream);
  1593. session = lsm_d->lsm_session;
  1594. lsm_ops = &cpe->lsm_ops;
  1595. if (p_info->param_size != sizeof(gain)) {
  1596. dev_err(rtd->dev,
  1597. "%s: Invalid param_size %d\n",
  1598. __func__, p_info->param_size);
  1599. rc = -EINVAL;
  1600. goto done;
  1601. }
  1602. if (copy_from_user(&gain, p_info->param_data,
  1603. p_info->param_size)) {
  1604. dev_err(rtd->dev,
  1605. "%s: copy_from_user failed, size = %d\n",
  1606. __func__, p_info->param_size);
  1607. rc = -EFAULT;
  1608. goto done;
  1609. }
  1610. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1611. session, p_info, &gain,
  1612. LSM_GAIN);
  1613. if (unlikely(rc))
  1614. dev_err(rtd->dev,
  1615. "%s: set_one_param(epd_threshold) failed, rc %d\n",
  1616. __func__, rc);
  1617. done:
  1618. return rc;
  1619. }
  1620. static int msm_cpe_lsm_set_conf(struct snd_pcm_substream *substream,
  1621. struct lsm_params_info *p_info)
  1622. {
  1623. struct snd_soc_pcm_runtime *rtd;
  1624. struct cpe_lsm_data *lsm_d = NULL;
  1625. struct cpe_priv *cpe = NULL;
  1626. struct cpe_lsm_session *session = NULL;
  1627. struct wcd_cpe_lsm_ops *lsm_ops;
  1628. int rc;
  1629. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1630. return -EINVAL;
  1631. rtd = substream->private_data;
  1632. lsm_d = cpe_get_lsm_data(substream);
  1633. cpe = cpe_get_private_data(substream);
  1634. session = lsm_d->lsm_session;
  1635. lsm_ops = &cpe->lsm_ops;
  1636. session->num_confidence_levels =
  1637. p_info->param_size;
  1638. rc = msm_cpe_lsm_get_conf_levels(session,
  1639. p_info->param_data);
  1640. if (rc) {
  1641. dev_err(rtd->dev,
  1642. "%s: get_conf_levels failed, err = %d\n",
  1643. __func__, rc);
  1644. goto done;
  1645. }
  1646. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1647. session, p_info, NULL,
  1648. LSM_MIN_CONFIDENCE_LEVELS);
  1649. if (unlikely(rc))
  1650. dev_err(rtd->dev,
  1651. "%s: set_one_param(conf_levels) failed, rc %d\n",
  1652. __func__, rc);
  1653. done:
  1654. return rc;
  1655. }
  1656. static int msm_cpe_lsm_reg_model(struct snd_pcm_substream *substream,
  1657. struct lsm_params_info *p_info)
  1658. {
  1659. struct snd_soc_pcm_runtime *rtd;
  1660. struct cpe_lsm_data *lsm_d = NULL;
  1661. struct cpe_priv *cpe = NULL;
  1662. struct cpe_lsm_session *session = NULL;
  1663. struct wcd_cpe_lsm_ops *lsm_ops;
  1664. int rc;
  1665. size_t offset;
  1666. u8 *snd_model_ptr;
  1667. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1668. return -EINVAL;
  1669. rtd = substream->private_data;
  1670. lsm_d = cpe_get_lsm_data(substream);
  1671. cpe = cpe_get_private_data(substream);
  1672. session = lsm_d->lsm_session;
  1673. lsm_ops = &cpe->lsm_ops;
  1674. lsm_ops->lsm_get_snd_model_offset(cpe->core_handle,
  1675. session, &offset);
  1676. /* Check if 'p_info->param_size + offset' crosses U32_MAX. */
  1677. if (p_info->param_size > U32_MAX - offset) {
  1678. dev_err(rtd->dev,
  1679. "%s: Invalid param_size %d\n",
  1680. __func__, p_info->param_size);
  1681. return -EINVAL;
  1682. }
  1683. session->snd_model_size = p_info->param_size + offset;
  1684. session->snd_model_data = vzalloc(session->snd_model_size);
  1685. if (!session->snd_model_data)
  1686. return -ENOMEM;
  1687. snd_model_ptr = ((u8 *) session->snd_model_data) + offset;
  1688. if (copy_from_user(snd_model_ptr,
  1689. p_info->param_data, p_info->param_size)) {
  1690. dev_err(rtd->dev,
  1691. "%s: copy_from_user for snd_model failed\n",
  1692. __func__);
  1693. rc = -EFAULT;
  1694. goto free_snd_model_data;
  1695. }
  1696. rc = lsm_ops->lsm_shmem_alloc(cpe->core_handle, session,
  1697. session->snd_model_size);
  1698. if (rc != 0) {
  1699. dev_err(rtd->dev,
  1700. "%s: shared memory allocation failed, err = %d\n",
  1701. __func__, rc);
  1702. rc = -EINVAL;
  1703. goto free_snd_model_data;
  1704. }
  1705. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1706. session, p_info, NULL,
  1707. LSM_REG_SND_MODEL);
  1708. if (unlikely(rc)) {
  1709. dev_err(rtd->dev,
  1710. "%s: set_one_param(snd_model) failed, rc %d\n",
  1711. __func__, rc);
  1712. goto dealloc_shmem;
  1713. }
  1714. return 0;
  1715. dealloc_shmem:
  1716. lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
  1717. free_snd_model_data:
  1718. vfree(session->snd_model_data);
  1719. return rc;
  1720. }
  1721. static int msm_cpe_lsm_dereg_model(struct snd_pcm_substream *substream,
  1722. struct lsm_params_info *p_info)
  1723. {
  1724. struct snd_soc_pcm_runtime *rtd;
  1725. struct cpe_lsm_data *lsm_d = NULL;
  1726. struct cpe_priv *cpe = NULL;
  1727. struct cpe_lsm_session *session = NULL;
  1728. struct wcd_cpe_lsm_ops *lsm_ops;
  1729. int rc;
  1730. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1731. return -EINVAL;
  1732. rtd = substream->private_data;
  1733. lsm_d = cpe_get_lsm_data(substream);
  1734. cpe = cpe_get_private_data(substream);
  1735. session = lsm_d->lsm_session;
  1736. lsm_ops = &cpe->lsm_ops;
  1737. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1738. session, p_info, NULL,
  1739. LSM_DEREG_SND_MODEL);
  1740. if (rc)
  1741. dev_err(rtd->dev,
  1742. "%s: dereg_snd_model failed\n",
  1743. __func__);
  1744. return lsm_ops->lsm_shmem_dealloc(cpe->core_handle, session);
  1745. }
  1746. static int msm_cpe_lsm_set_custom(struct snd_pcm_substream *substream,
  1747. struct lsm_params_info *p_info)
  1748. {
  1749. struct snd_soc_pcm_runtime *rtd;
  1750. struct cpe_lsm_data *lsm_d = NULL;
  1751. struct cpe_priv *cpe = NULL;
  1752. struct cpe_lsm_session *session = NULL;
  1753. struct wcd_cpe_lsm_ops *lsm_ops;
  1754. u8 *data;
  1755. int rc;
  1756. if (!msm_cpe_lsm_is_valid_stream(substream, __func__))
  1757. return -EINVAL;
  1758. rtd = substream->private_data;
  1759. lsm_d = cpe_get_lsm_data(substream);
  1760. cpe = cpe_get_private_data(substream);
  1761. session = lsm_d->lsm_session;
  1762. lsm_ops = &cpe->lsm_ops;
  1763. if (p_info->param_size > MSM_CPE_MAX_CUSTOM_PARAM_SIZE) {
  1764. dev_err(rtd->dev,
  1765. "%s: invalid size %d, max allowed %d\n",
  1766. __func__, p_info->param_size,
  1767. MSM_CPE_MAX_CUSTOM_PARAM_SIZE);
  1768. return -EINVAL;
  1769. }
  1770. data = kzalloc(p_info->param_size, GFP_KERNEL);
  1771. if (!data)
  1772. return -ENOMEM;
  1773. if (copy_from_user(data, p_info->param_data,
  1774. p_info->param_size)) {
  1775. dev_err(rtd->dev,
  1776. "%s: copy_from_user failed for custom params, size = %d\n",
  1777. __func__, p_info->param_size);
  1778. rc = -EFAULT;
  1779. goto err_ret;
  1780. }
  1781. rc = lsm_ops->lsm_set_one_param(cpe->core_handle,
  1782. session, p_info, data,
  1783. LSM_CUSTOM_PARAMS);
  1784. if (rc)
  1785. dev_err(rtd->dev,
  1786. "%s: custom_params failed, err = %d\n",
  1787. __func__, rc);
  1788. err_ret:
  1789. kfree(data);
  1790. return rc;
  1791. }
  1792. static int msm_cpe_lsm_process_params(struct snd_pcm_substream *substream,
  1793. struct snd_lsm_module_params *p_data,
  1794. void *params)
  1795. {
  1796. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  1797. struct lsm_params_info *p_info;
  1798. int i;
  1799. int rc = 0;
  1800. p_info = (struct lsm_params_info *) params;
  1801. for (i = 0; i < p_data->num_params; i++) {
  1802. dev_dbg(rtd->dev,
  1803. "%s: param (%d), module_id = 0x%x, param_id = 0x%x, param_size = 0x%x, param_type = 0x%x\n",
  1804. __func__, i, p_info->module_id,
  1805. p_info->param_id, p_info->param_size,
  1806. p_info->param_type);
  1807. switch (p_info->param_type) {
  1808. case LSM_ENDPOINT_DETECT_THRESHOLD:
  1809. rc = msm_cpe_lsm_set_epd(substream, p_info);
  1810. break;
  1811. case LSM_OPERATION_MODE:
  1812. rc = msm_cpe_lsm_set_mode(substream, p_info);
  1813. break;
  1814. case LSM_GAIN:
  1815. rc = msm_cpe_lsm_set_gain(substream, p_info);
  1816. break;
  1817. case LSM_MIN_CONFIDENCE_LEVELS:
  1818. rc = msm_cpe_lsm_set_conf(substream, p_info);
  1819. break;
  1820. case LSM_REG_SND_MODEL:
  1821. rc = msm_cpe_lsm_reg_model(substream, p_info);
  1822. break;
  1823. case LSM_DEREG_SND_MODEL:
  1824. rc = msm_cpe_lsm_dereg_model(substream, p_info);
  1825. break;
  1826. case LSM_CUSTOM_PARAMS:
  1827. rc = msm_cpe_lsm_set_custom(substream, p_info);
  1828. break;
  1829. default:
  1830. dev_err(rtd->dev,
  1831. "%s: Invalid param_type %d\n",
  1832. __func__, p_info->param_type);
  1833. rc = -EINVAL;
  1834. break;
  1835. }
  1836. if (rc) {
  1837. pr_err("%s: set_param fail for param_type %d\n",
  1838. __func__, p_info->param_type);
  1839. return rc;
  1840. }
  1841. p_info++;
  1842. }
  1843. return rc;
  1844. }
  1845. static int msm_cpe_lsm_ioctl(struct snd_pcm_substream *substream,
  1846. unsigned int cmd, void *arg)
  1847. {
  1848. int err = 0;
  1849. struct snd_soc_pcm_runtime *rtd;
  1850. struct cpe_priv *cpe = NULL;
  1851. struct cpe_lsm_data *lsm_d = NULL;
  1852. struct cpe_lsm_session *session = NULL;
  1853. struct wcd_cpe_lsm_ops *lsm_ops;
  1854. if (!substream || !substream->private_data) {
  1855. pr_err("%s: invalid substream (%pK)\n",
  1856. __func__, substream);
  1857. return -EINVAL;
  1858. }
  1859. rtd = substream->private_data;
  1860. lsm_d = cpe_get_lsm_data(substream);
  1861. cpe = cpe_get_private_data(substream);
  1862. if (!cpe || !cpe->core_handle) {
  1863. dev_err(rtd->dev,
  1864. "%s: Invalid private data\n",
  1865. __func__);
  1866. return -EINVAL;
  1867. }
  1868. if (!lsm_d || !lsm_d->lsm_session) {
  1869. dev_err(rtd->dev,
  1870. "%s: Invalid session data\n",
  1871. __func__);
  1872. return -EINVAL;
  1873. }
  1874. MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
  1875. "lsm_api_lock");
  1876. session = lsm_d->lsm_session;
  1877. lsm_ops = &cpe->lsm_ops;
  1878. switch (cmd) {
  1879. case SNDRV_LSM_REG_SND_MODEL_V2: {
  1880. struct snd_lsm_sound_model_v2 snd_model;
  1881. if (session->is_topology_used) {
  1882. dev_err(rtd->dev,
  1883. "%s: %s: not supported if using topology\n",
  1884. __func__, "LSM_REG_SND_MODEL_V2");
  1885. err = -EINVAL;
  1886. goto done;
  1887. }
  1888. if (copy_from_user(&snd_model, (void *)arg,
  1889. sizeof(struct snd_lsm_sound_model_v2))) {
  1890. dev_err(rtd->dev,
  1891. "%s: copy from user failed, size %zd\n",
  1892. __func__,
  1893. sizeof(struct snd_lsm_sound_model_v2));
  1894. err = -EFAULT;
  1895. goto done;
  1896. }
  1897. err = msm_cpe_lsm_ioctl_shared(substream, cmd,
  1898. &snd_model);
  1899. }
  1900. break;
  1901. case SNDRV_LSM_EVENT_STATUS: {
  1902. struct snd_lsm_event_status u_event_status;
  1903. struct snd_lsm_event_status *event_status = NULL;
  1904. int u_pld_size = 0;
  1905. if (copy_from_user(&u_event_status, (void *)arg,
  1906. sizeof(struct snd_lsm_event_status))) {
  1907. dev_err(rtd->dev,
  1908. "%s: event status copy from user failed, size %zd\n",
  1909. __func__,
  1910. sizeof(struct snd_lsm_event_status));
  1911. err = -EFAULT;
  1912. goto done;
  1913. }
  1914. if (u_event_status.payload_size >
  1915. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  1916. dev_err(rtd->dev,
  1917. "%s: payload_size %d is invalid, max allowed = %d\n",
  1918. __func__, u_event_status.payload_size,
  1919. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  1920. err = -EINVAL;
  1921. goto done;
  1922. }
  1923. u_pld_size = sizeof(struct snd_lsm_event_status) +
  1924. u_event_status.payload_size;
  1925. event_status = kzalloc(u_pld_size, GFP_KERNEL);
  1926. if (!event_status) {
  1927. err = -ENOMEM;
  1928. goto done;
  1929. } else {
  1930. event_status->payload_size =
  1931. u_event_status.payload_size;
  1932. err = msm_cpe_lsm_ioctl_shared(substream,
  1933. cmd, event_status);
  1934. }
  1935. if (!err && copy_to_user(arg, event_status, u_pld_size)) {
  1936. dev_err(rtd->dev,
  1937. "%s: copy to user failed\n",
  1938. __func__);
  1939. kfree(event_status);
  1940. err = -EFAULT;
  1941. goto done;
  1942. }
  1943. msm_cpe_lsm_lab_start(substream, event_status->status);
  1944. msm_cpe_process_event_status_done(lsm_d);
  1945. kfree(event_status);
  1946. }
  1947. break;
  1948. case SNDRV_LSM_EVENT_STATUS_V3: {
  1949. struct snd_lsm_event_status_v3 u_event_status;
  1950. struct snd_lsm_event_status_v3 *event_status = NULL;
  1951. int u_pld_size = 0;
  1952. if (copy_from_user(&u_event_status, (void *)arg,
  1953. sizeof(struct snd_lsm_event_status_v3))) {
  1954. dev_err(rtd->dev,
  1955. "%s: event status copy from user failed, size %zd\n",
  1956. __func__,
  1957. sizeof(struct snd_lsm_event_status_v3));
  1958. err = -EFAULT;
  1959. goto done;
  1960. }
  1961. if (u_event_status.payload_size >
  1962. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  1963. dev_err(rtd->dev,
  1964. "%s: payload_size %d is invalid, max allowed = %d\n",
  1965. __func__, u_event_status.payload_size,
  1966. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  1967. err = -EINVAL;
  1968. goto done;
  1969. }
  1970. u_pld_size = sizeof(struct snd_lsm_event_status_v3) +
  1971. u_event_status.payload_size;
  1972. event_status = kzalloc(u_pld_size, GFP_KERNEL);
  1973. if (!event_status) {
  1974. err = -ENOMEM;
  1975. goto done;
  1976. } else {
  1977. event_status->payload_size =
  1978. u_event_status.payload_size;
  1979. err = msm_cpe_lsm_ioctl_shared(substream,
  1980. cmd, event_status);
  1981. }
  1982. if (!err && copy_to_user(arg, event_status, u_pld_size)) {
  1983. dev_err(rtd->dev,
  1984. "%s: copy to user failed\n",
  1985. __func__);
  1986. kfree(event_status);
  1987. err = -EFAULT;
  1988. goto done;
  1989. }
  1990. msm_cpe_lsm_lab_start(substream, event_status->status);
  1991. msm_cpe_process_event_status_done(lsm_d);
  1992. kfree(event_status);
  1993. }
  1994. break;
  1995. case SNDRV_LSM_SET_PARAMS: {
  1996. struct snd_lsm_detection_params det_params;
  1997. if (session->is_topology_used) {
  1998. dev_err(rtd->dev,
  1999. "%s: %s: not supported if using topology\n",
  2000. __func__, "SNDRV_LSM_SET_PARAMS");
  2001. err = -EINVAL;
  2002. goto done;
  2003. }
  2004. if (copy_from_user(&det_params, (void *) arg,
  2005. sizeof(det_params))) {
  2006. dev_err(rtd->dev,
  2007. "%s: %s: copy_from_user failed, size = %zd\n",
  2008. __func__, "SNDRV_LSM_SET_PARAMS",
  2009. sizeof(det_params));
  2010. err = -EFAULT;
  2011. goto done;
  2012. }
  2013. err = msm_cpe_lsm_ioctl_shared(substream, cmd,
  2014. &det_params);
  2015. }
  2016. break;
  2017. case SNDRV_LSM_SET_MODULE_PARAMS: {
  2018. struct snd_lsm_module_params p_data;
  2019. size_t p_size;
  2020. u8 *params;
  2021. if (!session->is_topology_used) {
  2022. dev_err(rtd->dev,
  2023. "%s: %s: not supported if not using topology\n",
  2024. __func__, "SET_MODULE_PARAMS");
  2025. err = -EINVAL;
  2026. goto done;
  2027. }
  2028. if (!arg) {
  2029. dev_err(rtd->dev,
  2030. "%s: %s: No Param data to set\n",
  2031. __func__, "SET_MODULE_PARAMS");
  2032. err = -EINVAL;
  2033. goto done;
  2034. }
  2035. if (copy_from_user(&p_data, arg,
  2036. sizeof(p_data))) {
  2037. dev_err(rtd->dev,
  2038. "%s: %s: copy_from_user failed, size = %zd\n",
  2039. __func__, "p_data", sizeof(p_data));
  2040. err = -EFAULT;
  2041. goto done;
  2042. }
  2043. if (p_data.num_params > LSM_PARAMS_MAX) {
  2044. dev_err(rtd->dev,
  2045. "%s: %s: Invalid num_params %d\n",
  2046. __func__, "SET_MODULE_PARAMS",
  2047. p_data.num_params);
  2048. err = -EINVAL;
  2049. goto done;
  2050. }
  2051. p_size = p_data.num_params *
  2052. sizeof(struct lsm_params_info);
  2053. if (p_data.data_size != p_size) {
  2054. dev_err(rtd->dev,
  2055. "%s: %s: Invalid size %zd\n",
  2056. __func__, "SET_MODULE_PARAMS", p_size);
  2057. err = -EFAULT;
  2058. goto done;
  2059. }
  2060. params = kzalloc(p_size, GFP_KERNEL);
  2061. if (!params) {
  2062. err = -ENOMEM;
  2063. goto done;
  2064. }
  2065. if (copy_from_user(params, p_data.params,
  2066. p_data.data_size)) {
  2067. dev_err(rtd->dev,
  2068. "%s: %s: copy_from_user failed, size = %d\n",
  2069. __func__, "params", p_data.data_size);
  2070. kfree(params);
  2071. err = -EFAULT;
  2072. goto done;
  2073. }
  2074. err = msm_cpe_lsm_process_params(substream, &p_data, params);
  2075. if (err)
  2076. dev_err(rtd->dev,
  2077. "%s: %s: Failed to set params, err = %d\n",
  2078. __func__, "SET_MODULE_PARAMS", err);
  2079. kfree(params);
  2080. break;
  2081. }
  2082. default:
  2083. err = msm_cpe_lsm_ioctl_shared(substream, cmd, arg);
  2084. break;
  2085. }
  2086. done:
  2087. MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
  2088. "lsm_api_lock");
  2089. return err;
  2090. }
  2091. #ifdef CONFIG_COMPAT
  2092. struct snd_lsm_sound_model_v2_32 {
  2093. compat_uptr_t data;
  2094. compat_uptr_t confidence_level;
  2095. u32 data_size;
  2096. enum lsm_detection_mode detection_mode;
  2097. u8 num_confidence_levels;
  2098. bool detect_failure;
  2099. };
  2100. struct snd_lsm_detection_params_32 {
  2101. compat_uptr_t conf_level;
  2102. enum lsm_detection_mode detect_mode;
  2103. u8 num_confidence_levels;
  2104. bool detect_failure;
  2105. };
  2106. struct lsm_params_info_32 {
  2107. u32 module_id;
  2108. u32 param_id;
  2109. u32 param_size;
  2110. compat_uptr_t param_data;
  2111. uint32_t param_type;
  2112. };
  2113. struct snd_lsm_module_params_32 {
  2114. compat_uptr_t params;
  2115. u32 num_params;
  2116. u32 data_size;
  2117. };
  2118. enum {
  2119. SNDRV_LSM_REG_SND_MODEL_V2_32 =
  2120. _IOW('U', 0x07, struct snd_lsm_sound_model_v2_32),
  2121. SNDRV_LSM_SET_PARAMS32 =
  2122. _IOW('U', 0x0A, struct snd_lsm_detection_params_32),
  2123. SNDRV_LSM_SET_MODULE_PARAMS_32 =
  2124. _IOW('U', 0x0B, struct snd_lsm_module_params_32),
  2125. };
  2126. static int msm_cpe_lsm_ioctl_compat(struct snd_pcm_substream *substream,
  2127. unsigned int cmd, void *arg)
  2128. {
  2129. int err = 0;
  2130. struct snd_soc_pcm_runtime *rtd;
  2131. struct cpe_priv *cpe = NULL;
  2132. struct cpe_lsm_data *lsm_d = NULL;
  2133. struct cpe_lsm_session *session = NULL;
  2134. struct wcd_cpe_lsm_ops *lsm_ops;
  2135. if (!substream || !substream->private_data) {
  2136. pr_err("%s: invalid substream (%pK)\n",
  2137. __func__, substream);
  2138. return -EINVAL;
  2139. }
  2140. rtd = substream->private_data;
  2141. lsm_d = cpe_get_lsm_data(substream);
  2142. cpe = cpe_get_private_data(substream);
  2143. if (!cpe || !cpe->core_handle) {
  2144. dev_err(rtd->dev,
  2145. "%s: Invalid private data\n",
  2146. __func__);
  2147. return -EINVAL;
  2148. }
  2149. if (!lsm_d || !lsm_d->lsm_session) {
  2150. dev_err(rtd->dev,
  2151. "%s: Invalid session data\n",
  2152. __func__);
  2153. return -EINVAL;
  2154. }
  2155. MSM_CPE_LSM_GRAB_LOCK(&lsm_d->lsm_api_lock,
  2156. "lsm_api_lock");
  2157. session = lsm_d->lsm_session;
  2158. lsm_ops = &cpe->lsm_ops;
  2159. switch (cmd) {
  2160. case SNDRV_LSM_REG_SND_MODEL_V2_32: {
  2161. struct snd_lsm_sound_model_v2 snd_model;
  2162. struct snd_lsm_sound_model_v2_32 snd_model32;
  2163. if (session->is_topology_used) {
  2164. dev_err(rtd->dev,
  2165. "%s: %s: not supported if using topology\n",
  2166. __func__, "LSM_REG_SND_MODEL_V2_32");
  2167. err = -EINVAL;
  2168. goto done;
  2169. }
  2170. dev_dbg(rtd->dev,
  2171. "%s: ioctl %s\n", __func__,
  2172. "SNDRV_LSM_REG_SND_MODEL_V2_32");
  2173. if (copy_from_user(&snd_model32, (void *)arg,
  2174. sizeof(snd_model32))) {
  2175. dev_err(rtd->dev,
  2176. "%s: copy from user failed, size %zd\n",
  2177. __func__,
  2178. sizeof(snd_model32));
  2179. err = -EFAULT;
  2180. goto done;
  2181. }
  2182. snd_model.data = compat_ptr(snd_model32.data);
  2183. snd_model.confidence_level =
  2184. compat_ptr(snd_model32.confidence_level);
  2185. snd_model.data_size = snd_model32.data_size;
  2186. snd_model.detect_failure = snd_model32.detect_failure;
  2187. snd_model.num_confidence_levels =
  2188. snd_model32.num_confidence_levels;
  2189. snd_model.detection_mode = snd_model32.detection_mode;
  2190. cmd = SNDRV_LSM_REG_SND_MODEL_V2;
  2191. err = msm_cpe_lsm_ioctl_shared(substream, cmd, &snd_model);
  2192. if (err)
  2193. dev_err(rtd->dev,
  2194. "%s: %s failed, error = %d\n",
  2195. __func__,
  2196. "SNDRV_LSM_REG_SND_MODEL_V2_32",
  2197. err);
  2198. }
  2199. break;
  2200. case SNDRV_LSM_EVENT_STATUS: {
  2201. struct snd_lsm_event_status *event_status = NULL;
  2202. struct snd_lsm_event_status u_event_status32;
  2203. struct snd_lsm_event_status *udata_32 = NULL;
  2204. int u_pld_size = 0;
  2205. dev_dbg(rtd->dev,
  2206. "%s: ioctl %s\n", __func__,
  2207. "SNDRV_LSM_EVENT_STATUS32");
  2208. if (copy_from_user(&u_event_status32, (void *)arg,
  2209. sizeof(struct snd_lsm_event_status))) {
  2210. dev_err(rtd->dev,
  2211. "%s: event status copy from user failed, size %zd\n",
  2212. __func__,
  2213. sizeof(struct snd_lsm_event_status));
  2214. err = -EFAULT;
  2215. goto done;
  2216. }
  2217. if (u_event_status32.payload_size >
  2218. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  2219. dev_err(rtd->dev,
  2220. "%s: payload_size %d is invalid, max allowed = %d\n",
  2221. __func__, u_event_status32.payload_size,
  2222. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  2223. err = -EINVAL;
  2224. goto done;
  2225. }
  2226. u_pld_size = sizeof(struct snd_lsm_event_status) +
  2227. u_event_status32.payload_size;
  2228. event_status = kzalloc(u_pld_size, GFP_KERNEL);
  2229. if (!event_status) {
  2230. dev_err(rtd->dev,
  2231. "%s: No memory for event status\n",
  2232. __func__);
  2233. err = -ENOMEM;
  2234. goto done;
  2235. } else {
  2236. event_status->payload_size =
  2237. u_event_status32.payload_size;
  2238. err = msm_cpe_lsm_ioctl_shared(substream,
  2239. cmd, event_status);
  2240. if (err)
  2241. dev_err(rtd->dev,
  2242. "%s: %s failed, error = %d\n",
  2243. __func__,
  2244. "SNDRV_LSM_EVENT_STATUS32",
  2245. err);
  2246. }
  2247. if (!err) {
  2248. udata_32 = kzalloc(u_pld_size, GFP_KERNEL);
  2249. if (!udata_32) {
  2250. dev_err(rtd->dev,
  2251. "%s: nomem for udata\n",
  2252. __func__);
  2253. err = -EFAULT;
  2254. } else {
  2255. udata_32->status = event_status->status;
  2256. udata_32->payload_size =
  2257. event_status->payload_size;
  2258. memcpy(udata_32->payload,
  2259. event_status->payload,
  2260. u_pld_size);
  2261. }
  2262. }
  2263. if (!err && copy_to_user(arg, udata_32,
  2264. u_pld_size)) {
  2265. dev_err(rtd->dev,
  2266. "%s: copy to user failed\n",
  2267. __func__);
  2268. kfree(event_status);
  2269. kfree(udata_32);
  2270. err = -EFAULT;
  2271. goto done;
  2272. }
  2273. msm_cpe_lsm_lab_start(substream, event_status->status);
  2274. msm_cpe_process_event_status_done(lsm_d);
  2275. kfree(event_status);
  2276. kfree(udata_32);
  2277. }
  2278. break;
  2279. case SNDRV_LSM_EVENT_STATUS_V3: {
  2280. struct snd_lsm_event_status_v3 *event_status = NULL;
  2281. struct snd_lsm_event_status_v3 u_event_status32;
  2282. struct snd_lsm_event_status_v3 *udata_32 = NULL;
  2283. int u_pld_size = 0;
  2284. dev_dbg(rtd->dev,
  2285. "%s: ioctl %s\n", __func__,
  2286. "SNDRV_LSM_EVENT_STATUS_V3_32");
  2287. if (copy_from_user(&u_event_status32, (void *)arg,
  2288. sizeof(struct snd_lsm_event_status_v3))) {
  2289. dev_err(rtd->dev,
  2290. "%s: event status copy from user failed, size %zd\n",
  2291. __func__,
  2292. sizeof(struct snd_lsm_event_status_v3));
  2293. err = -EFAULT;
  2294. goto done;
  2295. }
  2296. if (u_event_status32.payload_size >
  2297. LISTEN_MAX_STATUS_PAYLOAD_SIZE) {
  2298. dev_err(rtd->dev,
  2299. "%s: payload_size %d is invalid, max allowed = %d\n",
  2300. __func__, u_event_status32.payload_size,
  2301. LISTEN_MAX_STATUS_PAYLOAD_SIZE);
  2302. err = -EINVAL;
  2303. goto done;
  2304. }
  2305. u_pld_size = sizeof(struct snd_lsm_event_status_v3) +
  2306. u_event_status32.payload_size;
  2307. event_status = kzalloc(u_pld_size, GFP_KERNEL);
  2308. if (!event_status) {
  2309. dev_err(rtd->dev,
  2310. "%s: No memory for event status\n",
  2311. __func__);
  2312. err = -ENOMEM;
  2313. goto done;
  2314. } else {
  2315. event_status->payload_size =
  2316. u_event_status32.payload_size;
  2317. err = msm_cpe_lsm_ioctl_shared(substream,
  2318. cmd, event_status);
  2319. if (err)
  2320. dev_err(rtd->dev,
  2321. "%s: %s failed, error = %d\n",
  2322. __func__,
  2323. "SNDRV_LSM_EVENT_STATUS_V3_32",
  2324. err);
  2325. }
  2326. if (!err) {
  2327. udata_32 = kzalloc(u_pld_size, GFP_KERNEL);
  2328. if (!udata_32) {
  2329. dev_err(rtd->dev,
  2330. "%s: nomem for udata\n",
  2331. __func__);
  2332. err = -EFAULT;
  2333. } else {
  2334. udata_32->timestamp_lsw =
  2335. event_status->timestamp_lsw;
  2336. udata_32->timestamp_msw =
  2337. event_status->timestamp_msw;
  2338. udata_32->status = event_status->status;
  2339. udata_32->payload_size =
  2340. event_status->payload_size;
  2341. memcpy(udata_32->payload,
  2342. event_status->payload,
  2343. u_pld_size);
  2344. }
  2345. }
  2346. if (!err && copy_to_user(arg, udata_32,
  2347. u_pld_size)) {
  2348. dev_err(rtd->dev,
  2349. "%s: copy to user failed\n",
  2350. __func__);
  2351. kfree(event_status);
  2352. kfree(udata_32);
  2353. err = -EFAULT;
  2354. goto done;
  2355. }
  2356. msm_cpe_lsm_lab_start(substream, event_status->status);
  2357. msm_cpe_process_event_status_done(lsm_d);
  2358. kfree(event_status);
  2359. kfree(udata_32);
  2360. }
  2361. break;
  2362. case SNDRV_LSM_SET_PARAMS32: {
  2363. struct snd_lsm_detection_params_32 det_params32;
  2364. struct snd_lsm_detection_params det_params;
  2365. if (session->is_topology_used) {
  2366. dev_err(rtd->dev,
  2367. "%s: %s: not supported if using topology\n",
  2368. __func__, "SNDRV_LSM_SET_PARAMS32");
  2369. err = -EINVAL;
  2370. goto done;
  2371. }
  2372. if (copy_from_user(&det_params32, arg,
  2373. sizeof(det_params32))) {
  2374. err = -EFAULT;
  2375. dev_err(rtd->dev,
  2376. "%s: %s: copy_from_user failed, size = %zd\n",
  2377. __func__, "SNDRV_LSM_SET_PARAMS_32",
  2378. sizeof(det_params32));
  2379. } else {
  2380. det_params.conf_level =
  2381. compat_ptr(det_params32.conf_level);
  2382. det_params.detect_mode =
  2383. det_params32.detect_mode;
  2384. det_params.num_confidence_levels =
  2385. det_params32.num_confidence_levels;
  2386. det_params.detect_failure =
  2387. det_params32.detect_failure;
  2388. cmd = SNDRV_LSM_SET_PARAMS;
  2389. err = msm_cpe_lsm_ioctl_shared(substream, cmd,
  2390. &det_params);
  2391. if (err)
  2392. dev_err(rtd->dev,
  2393. "%s: ioctl %s failed\n", __func__,
  2394. "SNDRV_LSM_SET_PARAMS");
  2395. }
  2396. break;
  2397. }
  2398. case SNDRV_LSM_SET_MODULE_PARAMS_32: {
  2399. struct snd_lsm_module_params_32 p_data_32;
  2400. struct snd_lsm_module_params p_data;
  2401. u8 *params, *params32;
  2402. size_t p_size;
  2403. struct lsm_params_info_32 *p_info_32;
  2404. struct lsm_params_info *p_info;
  2405. int i;
  2406. if (!session->is_topology_used) {
  2407. dev_err(rtd->dev,
  2408. "%s: %s: not supported if not using topology\n",
  2409. __func__, "SET_MODULE_PARAMS_32");
  2410. err = -EINVAL;
  2411. goto done;
  2412. }
  2413. if (copy_from_user(&p_data_32, arg,
  2414. sizeof(p_data_32))) {
  2415. dev_err(rtd->dev,
  2416. "%s: %s: copy_from_user failed, size = %zd\n",
  2417. __func__, "SET_MODULE_PARAMS_32",
  2418. sizeof(p_data_32));
  2419. err = -EFAULT;
  2420. goto done;
  2421. }
  2422. p_data.params = compat_ptr(p_data_32.params);
  2423. p_data.num_params = p_data_32.num_params;
  2424. p_data.data_size = p_data_32.data_size;
  2425. if (p_data.num_params > LSM_PARAMS_MAX) {
  2426. dev_err(rtd->dev,
  2427. "%s: %s: Invalid num_params %d\n",
  2428. __func__, "SET_MODULE_PARAMS_32",
  2429. p_data.num_params);
  2430. err = -EINVAL;
  2431. goto done;
  2432. }
  2433. if (p_data.data_size !=
  2434. (p_data.num_params * sizeof(struct lsm_params_info_32))) {
  2435. dev_err(rtd->dev,
  2436. "%s: %s: Invalid size %d\n",
  2437. __func__, "SET_MODULE_PARAMS_32",
  2438. p_data.data_size);
  2439. err = -EINVAL;
  2440. goto done;
  2441. }
  2442. p_size = sizeof(struct lsm_params_info_32) *
  2443. p_data.num_params;
  2444. params32 = kzalloc(p_size, GFP_KERNEL);
  2445. if (!params32) {
  2446. err = -ENOMEM;
  2447. goto done;
  2448. }
  2449. p_size = sizeof(struct lsm_params_info) * p_data.num_params;
  2450. params = kzalloc(p_size, GFP_KERNEL);
  2451. if (!params) {
  2452. kfree(params32);
  2453. err = -ENOMEM;
  2454. goto done;
  2455. }
  2456. if (copy_from_user(params32, p_data.params,
  2457. p_data.data_size)) {
  2458. dev_err(rtd->dev,
  2459. "%s: %s: copy_from_user failed, size = %d\n",
  2460. __func__, "params32", p_data.data_size);
  2461. kfree(params32);
  2462. kfree(params);
  2463. err = -EFAULT;
  2464. goto done;
  2465. }
  2466. p_info_32 = (struct lsm_params_info_32 *) params32;
  2467. p_info = (struct lsm_params_info *) params;
  2468. for (i = 0; i < p_data.num_params; i++) {
  2469. p_info->module_id = p_info_32->module_id;
  2470. p_info->param_id = p_info_32->param_id;
  2471. p_info->param_size = p_info_32->param_size;
  2472. p_info->param_data = compat_ptr(p_info_32->param_data);
  2473. p_info->param_type = p_info_32->param_type;
  2474. p_info_32++;
  2475. p_info++;
  2476. }
  2477. err = msm_cpe_lsm_process_params(substream,
  2478. &p_data, params);
  2479. if (err)
  2480. dev_err(rtd->dev,
  2481. "%s: Failed to process params, err = %d\n",
  2482. __func__, err);
  2483. kfree(params);
  2484. kfree(params32);
  2485. break;
  2486. }
  2487. case SNDRV_LSM_REG_SND_MODEL_V2:
  2488. case SNDRV_LSM_SET_PARAMS:
  2489. case SNDRV_LSM_SET_MODULE_PARAMS:
  2490. /*
  2491. * In ideal cases, the compat_ioctl should never be called
  2492. * with the above unlocked ioctl commands. Print error
  2493. * and return error if it does.
  2494. */
  2495. dev_err(rtd->dev,
  2496. "%s: Invalid cmd for compat_ioctl\n",
  2497. __func__);
  2498. err = -EINVAL;
  2499. break;
  2500. default:
  2501. err = msm_cpe_lsm_ioctl_shared(substream, cmd, arg);
  2502. break;
  2503. }
  2504. done:
  2505. MSM_CPE_LSM_REL_LOCK(&lsm_d->lsm_api_lock,
  2506. "lsm_api_lock");
  2507. return err;
  2508. }
  2509. #else
  2510. #define msm_cpe_lsm_ioctl_compat NULL
  2511. #endif
  2512. /*
  2513. * msm_cpe_lsm_prepare: prepare call from ASoC core for this platform
  2514. * @substream: ASoC substream for which the operation is invoked
  2515. *
  2516. * start the AFE port on CPE associated for this listen session
  2517. */
  2518. static int msm_cpe_lsm_prepare(struct snd_pcm_substream *substream)
  2519. {
  2520. int rc = 0;
  2521. struct cpe_priv *cpe = cpe_get_private_data(substream);
  2522. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2523. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2524. struct wcd_cpe_afe_ops *afe_ops;
  2525. struct wcd_cpe_afe_port_cfg *afe_cfg;
  2526. struct cpe_lsm_session *lsm_session;
  2527. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  2528. struct snd_pcm_runtime *runtime = substream->runtime;
  2529. struct lsm_hw_params lsm_param;
  2530. struct wcd_cpe_lsm_ops *lsm_ops;
  2531. if (!cpe || !cpe->core_handle) {
  2532. dev_err(rtd->dev,
  2533. "%s: Invalid private data\n",
  2534. __func__);
  2535. return -EINVAL;
  2536. }
  2537. if (!lsm_d || !lsm_d->lsm_session) {
  2538. dev_err(rtd->dev,
  2539. "%s: Invalid session data\n",
  2540. __func__);
  2541. return -EINVAL;
  2542. }
  2543. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  2544. runtime->status->state == SNDRV_PCM_STATE_PREPARED) {
  2545. pr_err("%s: XRUN ignore for now\n", __func__);
  2546. return 0;
  2547. }
  2548. lsm_session = lsm_d->lsm_session;
  2549. lab_d->pcm_size = snd_pcm_lib_buffer_bytes(substream);
  2550. dev_dbg(rtd->dev,
  2551. "%s: pcm_size 0x%x", __func__, lab_d->pcm_size);
  2552. if (lsm_d->cpe_prepared) {
  2553. dev_dbg(rtd->dev, "%s: CPE is alredy prepared\n",
  2554. __func__);
  2555. return 0;
  2556. }
  2557. lsm_ops = &cpe->lsm_ops;
  2558. afe_ops = &cpe->afe_ops;
  2559. afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
  2560. switch (cpe->input_port_id) {
  2561. case AFE_PORT_ID_3:
  2562. afe_cfg->port_id = AFE_PORT_ID_3;
  2563. afe_cfg->bit_width = 16;
  2564. afe_cfg->num_channels = 1;
  2565. afe_cfg->sample_rate = SAMPLE_RATE_48KHZ;
  2566. rc = afe_ops->afe_port_cmd_cfg(cpe->core_handle, afe_cfg);
  2567. break;
  2568. case AFE_PORT_ID_1:
  2569. default:
  2570. afe_cfg->port_id = AFE_PORT_ID_1;
  2571. afe_cfg->bit_width = 16;
  2572. afe_cfg->num_channels = 1;
  2573. afe_cfg->sample_rate = SAMPLE_RATE_16KHZ;
  2574. rc = afe_ops->afe_set_params(cpe->core_handle,
  2575. afe_cfg, cpe->afe_mad_ctl);
  2576. break;
  2577. }
  2578. if (rc != 0) {
  2579. dev_err(rtd->dev,
  2580. "%s: cpe afe params failed for port = %d, err = %d\n",
  2581. __func__, afe_cfg->port_id, rc);
  2582. return rc;
  2583. }
  2584. lsm_param.sample_rate = afe_cfg->sample_rate;
  2585. lsm_param.num_chs = afe_cfg->num_channels;
  2586. lsm_param.bit_width = afe_cfg->bit_width;
  2587. rc = lsm_ops->lsm_set_media_fmt_params(cpe->core_handle, lsm_session,
  2588. &lsm_param);
  2589. if (rc)
  2590. dev_dbg(rtd->dev,
  2591. "%s: failed to set lsm media fmt params, err = %d\n",
  2592. __func__, rc);
  2593. /* Send connect to port (input) */
  2594. rc = lsm_ops->lsm_set_port(cpe->core_handle, lsm_session,
  2595. &cpe->input_port_id);
  2596. if (rc) {
  2597. dev_err(rtd->dev,
  2598. "%s: Failed to set connect input port, err=%d\n",
  2599. __func__, rc);
  2600. return rc;
  2601. }
  2602. if (cpe->input_port_id != 3) {
  2603. rc = lsm_ops->lsm_get_afe_out_port_id(cpe->core_handle,
  2604. lsm_session);
  2605. if (rc != 0) {
  2606. dev_err(rtd->dev,
  2607. "%s: failed to get port id, err = %d\n",
  2608. __func__, rc);
  2609. return rc;
  2610. }
  2611. /* Send connect to port (output) */
  2612. rc = lsm_ops->lsm_set_port(cpe->core_handle, lsm_session,
  2613. &lsm_session->afe_out_port_id);
  2614. if (rc) {
  2615. dev_err(rtd->dev,
  2616. "%s: Failed to set connect output port, err=%d\n",
  2617. __func__, rc);
  2618. return rc;
  2619. }
  2620. }
  2621. rc = msm_cpe_afe_port_cntl(substream,
  2622. cpe->core_handle,
  2623. afe_ops, afe_cfg,
  2624. AFE_CMD_PORT_START);
  2625. if (rc)
  2626. dev_err(rtd->dev,
  2627. "%s: cpe_afe_port start failed, err = %d\n",
  2628. __func__, rc);
  2629. else
  2630. lsm_d->cpe_prepared = true;
  2631. return rc;
  2632. }
  2633. /*
  2634. * msm_cpe_lsm_trigger: trigger call from ASoC core for this platform
  2635. * @substream: ASoC substream for which the operation is invoked
  2636. * @cmd: the trigger command from framework
  2637. *
  2638. * suspend/resume the AFE port on CPE associated with listen session
  2639. */
  2640. static int msm_cpe_lsm_trigger(struct snd_pcm_substream *substream,
  2641. int cmd)
  2642. {
  2643. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2644. struct cpe_priv *cpe = cpe_get_private_data(substream);
  2645. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2646. struct wcd_cpe_afe_ops *afe_ops;
  2647. struct wcd_cpe_afe_port_cfg *afe_cfg;
  2648. int afe_cmd = AFE_CMD_INVALID;
  2649. int rc = 0;
  2650. if (!cpe || !cpe->core_handle) {
  2651. dev_err(rtd->dev,
  2652. "%s: Invalid private data\n",
  2653. __func__);
  2654. return -EINVAL;
  2655. }
  2656. if (!lsm_d || !lsm_d->lsm_session) {
  2657. dev_err(rtd->dev,
  2658. "%s: Invalid session data\n",
  2659. __func__);
  2660. return -EINVAL;
  2661. }
  2662. afe_ops = &cpe->afe_ops;
  2663. afe_cfg = &(lsm_d->lsm_session->afe_port_cfg);
  2664. switch (cmd) {
  2665. case SNDRV_PCM_TRIGGER_SUSPEND:
  2666. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  2667. afe_cmd = AFE_CMD_PORT_SUSPEND;
  2668. break;
  2669. case SNDRV_PCM_TRIGGER_RESUME:
  2670. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  2671. afe_cmd = AFE_CMD_PORT_RESUME;
  2672. break;
  2673. default:
  2674. afe_cmd = AFE_CMD_INVALID;
  2675. dev_dbg(rtd->dev,
  2676. "%s: unhandled trigger cmd %d\n",
  2677. __func__, cmd);
  2678. break;
  2679. }
  2680. if (afe_cmd != AFE_CMD_INVALID)
  2681. rc = msm_cpe_afe_port_cntl(substream,
  2682. cpe->core_handle,
  2683. afe_ops, afe_cfg,
  2684. afe_cmd);
  2685. return rc;
  2686. }
  2687. static int msm_cpe_lsm_hwparams(struct snd_pcm_substream *substream,
  2688. struct snd_pcm_hw_params *params)
  2689. {
  2690. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2691. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2692. struct cpe_priv *cpe = cpe_get_private_data(substream);
  2693. struct cpe_lsm_session *session = NULL;
  2694. struct cpe_hw_params *hw_params = NULL;
  2695. if (!cpe || !cpe->core_handle) {
  2696. dev_err(rtd->dev,
  2697. "%s: Invalid %s\n",
  2698. __func__,
  2699. (!cpe) ? "cpe" : "core");
  2700. return -EINVAL;
  2701. }
  2702. if (!lsm_d || !lsm_d->lsm_session) {
  2703. dev_err(rtd->dev,
  2704. "%s: Invalid %s\n",
  2705. __func__,
  2706. (!lsm_d) ? "priv_data" : "session");
  2707. return -EINVAL;
  2708. }
  2709. session = lsm_d->lsm_session;
  2710. hw_params = &lsm_d->hw_params;
  2711. hw_params->buf_sz = (params_buffer_bytes(params)
  2712. / params_periods(params));
  2713. hw_params->period_count = params_periods(params);
  2714. hw_params->channels = params_channels(params);
  2715. hw_params->sample_rate = params_rate(params);
  2716. if (params_format(params) == SNDRV_PCM_FORMAT_S16_LE)
  2717. hw_params->sample_size = 16;
  2718. else if (params_format(params) ==
  2719. SNDRV_PCM_FORMAT_S24_LE)
  2720. hw_params->sample_size = 24;
  2721. else if (params_format(params) ==
  2722. SNDRV_PCM_FORMAT_S32_LE)
  2723. hw_params->sample_size = 32;
  2724. else {
  2725. dev_err(rtd->dev,
  2726. "%s: Invalid Format 0x%x\n",
  2727. __func__, params_format(params));
  2728. return -EINVAL;
  2729. }
  2730. dev_dbg(rtd->dev,
  2731. "%s: Format %d buffer size(bytes) %d period count %d\n"
  2732. " Channel %d period in bytes 0x%x Period Size 0x%x rate = %d\n",
  2733. __func__, params_format(params), params_buffer_bytes(params),
  2734. params_periods(params), params_channels(params),
  2735. params_period_bytes(params), params_period_size(params),
  2736. params_rate(params));
  2737. return 0;
  2738. }
  2739. static snd_pcm_uframes_t msm_cpe_lsm_pointer(
  2740. struct snd_pcm_substream *substream)
  2741. {
  2742. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2743. struct snd_pcm_runtime *runtime = substream->runtime;
  2744. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2745. struct cpe_lsm_session *session;
  2746. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  2747. session = lsm_d->lsm_session;
  2748. if (lab_d->dma_write >= lab_d->pcm_size)
  2749. lab_d->dma_write = 0;
  2750. dev_dbg(rtd->dev,
  2751. "%s:pcm_dma_pos = %d\n",
  2752. __func__, lab_d->dma_write);
  2753. return bytes_to_frames(runtime, (lab_d->dma_write));
  2754. }
  2755. static int msm_cpe_lsm_copy(struct snd_pcm_substream *substream, int a,
  2756. unsigned long hwoff, void __user *buf, unsigned long fbytes)
  2757. {
  2758. struct cpe_lsm_data *lsm_d = cpe_get_lsm_data(substream);
  2759. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  2760. struct snd_pcm_runtime *runtime = substream->runtime;
  2761. struct cpe_lsm_session *session;
  2762. struct cpe_lsm_lab *lab_d = &lsm_d->lab;
  2763. char *pcm_buf;
  2764. int rc = 0;
  2765. if (runtime->status->state == SNDRV_PCM_STATE_XRUN ||
  2766. runtime->status->state == SNDRV_PCM_STATE_PREPARED) {
  2767. pr_err("%s: XRUN ignore for now\n", __func__);
  2768. return 0;
  2769. }
  2770. session = lsm_d->lsm_session;
  2771. /* Check if buffer reading is already in error state */
  2772. if (lab_d->thread_status == MSM_LSM_LAB_THREAD_ERROR) {
  2773. dev_err(rtd->dev,
  2774. "%s: Bufferring is in error state\n",
  2775. __func__);
  2776. /*
  2777. * Advance the period so there is no wait in case
  2778. * read is invoked even after error is propogated
  2779. */
  2780. atomic_inc(&lab_d->in_count);
  2781. lab_d->dma_write += snd_pcm_lib_period_bytes(substream);
  2782. snd_pcm_period_elapsed(substream);
  2783. return -ENETRESET;
  2784. } else if (lab_d->thread_status == MSM_LSM_LAB_THREAD_STOP) {
  2785. dev_err(rtd->dev,
  2786. "%s: Buferring is in stopped\n",
  2787. __func__);
  2788. return -EIO;
  2789. }
  2790. rc = wait_event_timeout(lab_d->period_wait,
  2791. (atomic_read(&lab_d->in_count) ||
  2792. atomic_read(&lab_d->abort_read)),
  2793. (2 * HZ));
  2794. if (atomic_read(&lab_d->abort_read)) {
  2795. pr_debug("%s: LSM LAB Abort read\n", __func__);
  2796. return -EIO;
  2797. }
  2798. if (lab_d->thread_status != MSM_LSM_LAB_THREAD_RUNNING) {
  2799. pr_err("%s: Lab stopped\n", __func__);
  2800. return -EIO;
  2801. }
  2802. if (!rc) {
  2803. pr_err("%s:LAB err wait_event_timeout\n", __func__);
  2804. rc = -EAGAIN;
  2805. goto fail;
  2806. }
  2807. if (lab_d->buf_idx >= (lsm_d->hw_params.period_count))
  2808. lab_d->buf_idx = 0;
  2809. pcm_buf = (lab_d->pcm_buf[lab_d->buf_idx].mem);
  2810. pr_debug("%s: Buf IDX = 0x%x pcm_buf %pK\n",
  2811. __func__, lab_d->buf_idx, pcm_buf);
  2812. if (pcm_buf) {
  2813. if (copy_to_user(buf, pcm_buf, fbytes)) {
  2814. pr_err("Failed to copy buf to user\n");
  2815. rc = -EFAULT;
  2816. goto fail;
  2817. }
  2818. }
  2819. lab_d->buf_idx++;
  2820. atomic_dec(&lab_d->in_count);
  2821. return 0;
  2822. fail:
  2823. return rc;
  2824. }
  2825. /*
  2826. * msm_asoc_cpe_lsm_probe: ASoC framework for lsm platform driver
  2827. * @platform: platform registered with ASoC core
  2828. *
  2829. * Allocate the private data for this platform and obtain the ops for
  2830. * lsm and afe modules from underlying driver. Also find the codec
  2831. * for this platform as specified by machine driver for ASoC framework.
  2832. */
  2833. static int msm_asoc_cpe_lsm_probe(struct snd_soc_platform *platform)
  2834. {
  2835. struct snd_soc_card *card;
  2836. struct snd_soc_pcm_runtime *rtd;
  2837. struct snd_soc_codec *codec;
  2838. struct cpe_priv *cpe_priv;
  2839. const struct snd_kcontrol_new *kcontrol;
  2840. bool found_runtime = false;
  2841. const char *cpe_dev_id = "qcom,msm-cpe-lsm-id";
  2842. u32 port_id = 0;
  2843. int ret = 0;
  2844. if (!platform || !platform->component.card) {
  2845. pr_err("%s: Invalid platform or card\n",
  2846. __func__);
  2847. return -EINVAL;
  2848. }
  2849. card = platform->component.card;
  2850. /* Match platform to codec */
  2851. list_for_each_entry(rtd, &card->rtd_list, list) {
  2852. if (!rtd->platform)
  2853. continue;
  2854. if (!strcmp(rtd->platform->component.name,
  2855. platform->component.name)) {
  2856. found_runtime = true;
  2857. break;
  2858. }
  2859. }
  2860. if (!found_runtime) {
  2861. dev_err(platform->dev,
  2862. "%s: Failed to find runtime for platform\n",
  2863. __func__);
  2864. return -EINVAL;
  2865. }
  2866. ret = of_property_read_u32(platform->dev->of_node, cpe_dev_id,
  2867. &port_id);
  2868. if (ret) {
  2869. dev_dbg(platform->dev,
  2870. "%s: missing 0x%x in dt node\n", __func__, port_id);
  2871. port_id = 1;
  2872. }
  2873. codec = rtd->codec;
  2874. cpe_priv = kzalloc(sizeof(struct cpe_priv),
  2875. GFP_KERNEL);
  2876. if (!cpe_priv)
  2877. return -ENOMEM;
  2878. cpe_priv->codec = codec;
  2879. cpe_priv->input_port_id = port_id;
  2880. wcd_cpe_get_lsm_ops(&cpe_priv->lsm_ops);
  2881. wcd_cpe_get_afe_ops(&cpe_priv->afe_ops);
  2882. snd_soc_platform_set_drvdata(platform, cpe_priv);
  2883. kcontrol = &msm_cpe_kcontrols[0];
  2884. snd_ctl_add(card->snd_card, snd_ctl_new1(kcontrol, cpe_priv));
  2885. return 0;
  2886. }
  2887. static const struct snd_pcm_ops msm_cpe_lsm_ops = {
  2888. .open = msm_cpe_lsm_open,
  2889. .close = msm_cpe_lsm_close,
  2890. .ioctl = msm_cpe_lsm_ioctl,
  2891. .prepare = msm_cpe_lsm_prepare,
  2892. .trigger = msm_cpe_lsm_trigger,
  2893. .pointer = msm_cpe_lsm_pointer,
  2894. .copy_user = msm_cpe_lsm_copy,
  2895. .hw_params = msm_cpe_lsm_hwparams,
  2896. .compat_ioctl = msm_cpe_lsm_ioctl_compat,
  2897. };
  2898. static struct snd_soc_platform_driver msm_soc_cpe_platform = {
  2899. .ops = &msm_cpe_lsm_ops,
  2900. .probe = msm_asoc_cpe_lsm_probe,
  2901. };
  2902. /*
  2903. * msm_cpe_lsm_probe: platform driver probe
  2904. * @pdev: platform device
  2905. *
  2906. * Register the ASoC platform driver with ASoC core
  2907. */
  2908. static int msm_cpe_lsm_probe(struct platform_device *pdev)
  2909. {
  2910. return snd_soc_register_platform(&pdev->dev,
  2911. &msm_soc_cpe_platform);
  2912. }
  2913. /*
  2914. * msm_cpe_lsm_remove: platform driver remove
  2915. * @pdev: platform device
  2916. *
  2917. * Deregister the ASoC platform driver
  2918. */
  2919. static int msm_cpe_lsm_remove(struct platform_device *pdev)
  2920. {
  2921. snd_soc_unregister_platform(&pdev->dev);
  2922. return 0;
  2923. }
  2924. static const struct of_device_id msm_cpe_lsm_dt_match[] = {
  2925. {.compatible = "qcom,msm-cpe-lsm" },
  2926. { }
  2927. };
  2928. static struct platform_driver msm_cpe_lsm_driver = {
  2929. .driver = {
  2930. .name = "msm-cpe-lsm",
  2931. .owner = THIS_MODULE,
  2932. .of_match_table = of_match_ptr(msm_cpe_lsm_dt_match),
  2933. },
  2934. .probe = msm_cpe_lsm_probe,
  2935. .remove = msm_cpe_lsm_remove,
  2936. };
  2937. int __init msm_cpe_lsm_init(void)
  2938. {
  2939. return platform_driver_register(&msm_cpe_lsm_driver);
  2940. }
  2941. void __exit msm_cpe_lsm_exit(void)
  2942. {
  2943. platform_driver_unregister(&msm_cpe_lsm_driver);
  2944. }
  2945. module_init(msm_cpe_lsm_init);
  2946. module_exit(msm_cpe_lsm_exit);
  2947. MODULE_DESCRIPTION("CPE LSM platform driver");
  2948. MODULE_DEVICE_TABLE(of, msm_cpe_lsm_dt_match);
  2949. MODULE_LICENSE("GPL v2");