msm-lsm-client.c 63 KB

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