msm-lsm-client.c 68 KB

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