q6lsm.c 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357
  1. /*
  2. * Copyright (c) 2013-2018, 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/fs.h>
  14. #include <linux/mutex.h>
  15. #include <linux/wait.h>
  16. #include <linux/miscdevice.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/sched.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/delay.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/slab.h>
  23. #include <linux/debugfs.h>
  24. #include <linux/time.h>
  25. #include <linux/atomic.h>
  26. #include <sound/lsm_params.h>
  27. #include <asm/ioctls.h>
  28. #include <linux/memory.h>
  29. #include <dsp/msm_audio_ion.h>
  30. #include <dsp/apr_audio-v2.h>
  31. #include <dsp/q6core.h>
  32. #include <dsp/q6lsm.h>
  33. #include <dsp/q6afe-v2.h>
  34. #include <dsp/q6common.h>
  35. #include <dsp/audio_cal_utils.h>
  36. #include "adsp_err.h"
  37. #define APR_TIMEOUT (5 * HZ)
  38. #define LSM_ALIGN_BOUNDARY 512
  39. #define LSM_SAMPLE_RATE 16000
  40. #define QLSM_PARAM_ID_MINOR_VERSION 1
  41. #define QLSM_PARAM_ID_MINOR_VERSION_2 2
  42. static int lsm_afe_port;
  43. enum {
  44. LSM_CUSTOM_TOP_IDX,
  45. LSM_TOP_IDX,
  46. LSM_CAL_IDX,
  47. LSM_MAX_CAL_IDX
  48. };
  49. enum {
  50. CMD_STATE_CLEARED = 0,
  51. CMD_STATE_WAIT_RESP = 1,
  52. };
  53. enum {
  54. LSM_INVALID_SESSION_ID = 0,
  55. LSM_MIN_SESSION_ID = 1,
  56. LSM_MAX_SESSION_ID = 8,
  57. LSM_CONTROL_SESSION = 0x0F,
  58. };
  59. #define CHECK_SESSION(x) (x < LSM_MIN_SESSION_ID || x > LSM_MAX_SESSION_ID)
  60. struct lsm_common {
  61. void *apr;
  62. atomic_t apr_users;
  63. struct lsm_client common_client[LSM_MAX_SESSION_ID + 1];
  64. int set_custom_topology;
  65. struct cal_type_data *cal_data[LSM_MAX_CAL_IDX];
  66. struct mutex apr_lock;
  67. };
  68. static struct lsm_common lsm_common;
  69. /*
  70. * mmap_handle_p can point either client->sound_model.mem_map_handle or
  71. * lsm_common.mmap_handle_for_cal.
  72. * mmap_lock must be held while accessing this.
  73. */
  74. static spinlock_t mmap_lock;
  75. static uint32_t *mmap_handle_p;
  76. static spinlock_t lsm_session_lock;
  77. static struct lsm_client *lsm_session[LSM_MAX_SESSION_ID + 1];
  78. static int q6lsm_mmapcallback(struct apr_client_data *data, void *priv);
  79. static int q6lsm_send_cal(struct lsm_client *client, u32 set_params_opcode);
  80. static int q6lsm_memory_map_regions(struct lsm_client *client,
  81. dma_addr_t dma_addr_p, uint32_t dma_buf_sz,
  82. uint32_t *mmap_p);
  83. static int q6lsm_memory_unmap_regions(struct lsm_client *client,
  84. uint32_t handle);
  85. static int q6lsm_callback(struct apr_client_data *data, void *priv)
  86. {
  87. struct lsm_client *client = (struct lsm_client *)priv;
  88. uint32_t token;
  89. uint32_t *payload;
  90. if (!client || !data) {
  91. pr_err("%s: client %pK data %pK\n",
  92. __func__, client, data);
  93. WARN_ON(1);
  94. return -EINVAL;
  95. }
  96. if (data->opcode == RESET_EVENTS) {
  97. pr_debug("%s: SSR event received 0x%x, event 0x%x, proc 0x%x\n",
  98. __func__, data->opcode, data->reset_event,
  99. data->reset_proc);
  100. apr_reset(client->apr);
  101. client->apr = NULL;
  102. atomic_set(&client->cmd_state, CMD_STATE_CLEARED);
  103. wake_up(&client->cmd_wait);
  104. cal_utils_clear_cal_block_q6maps(LSM_MAX_CAL_IDX,
  105. lsm_common.cal_data);
  106. mutex_lock(&lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]->lock);
  107. lsm_common.set_custom_topology = 1;
  108. mutex_unlock(&lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]->lock);
  109. return 0;
  110. }
  111. payload = data->payload;
  112. pr_debug("%s: Session %d opcode 0x%x token 0x%x payload size %d\n"
  113. "payload [0] = 0x%x\n", __func__, client->session,
  114. data->opcode, data->token, data->payload_size, payload[0]);
  115. if (data->opcode == LSM_DATA_EVENT_READ_DONE) {
  116. struct lsm_cmd_read_done read_done;
  117. token = data->token;
  118. if (data->payload_size > sizeof(read_done)) {
  119. pr_err("%s: read done error payload size %d expected size %zd\n",
  120. __func__, data->payload_size,
  121. sizeof(read_done));
  122. return -EINVAL;
  123. }
  124. pr_debug("%s: opcode %x status %x lsw %x msw %x mem_map handle %x\n",
  125. __func__, data->opcode, payload[0], payload[1],
  126. payload[2], payload[3]);
  127. read_done.status = payload[0];
  128. read_done.buf_addr_lsw = payload[1];
  129. read_done.buf_addr_msw = payload[2];
  130. read_done.mem_map_handle = payload[3];
  131. read_done.total_size = payload[4];
  132. read_done.offset = payload[5];
  133. if (client->cb)
  134. client->cb(data->opcode, data->token,
  135. (void *)&read_done,
  136. client->priv);
  137. return 0;
  138. } else if (data->opcode == APR_BASIC_RSP_RESULT) {
  139. token = data->token;
  140. switch (payload[0]) {
  141. case LSM_SESSION_CMD_START:
  142. case LSM_SESSION_CMD_STOP:
  143. case LSM_SESSION_CMD_SET_PARAMS:
  144. case LSM_SESSION_CMD_OPEN_TX:
  145. case LSM_SESSION_CMD_CLOSE_TX:
  146. case LSM_SESSION_CMD_REGISTER_SOUND_MODEL:
  147. case LSM_SESSION_CMD_DEREGISTER_SOUND_MODEL:
  148. case LSM_SESSION_CMD_SHARED_MEM_UNMAP_REGIONS:
  149. case LSM_SESSION_CMD_EOB:
  150. case LSM_SESSION_CMD_READ:
  151. case LSM_SESSION_CMD_OPEN_TX_V2:
  152. case LSM_CMD_ADD_TOPOLOGIES:
  153. case LSM_SESSION_CMD_SET_PARAMS_V2:
  154. case LSM_SESSION_CMD_SET_PARAMS_V3:
  155. if (token != client->session &&
  156. payload[0] !=
  157. LSM_SESSION_CMD_DEREGISTER_SOUND_MODEL) {
  158. pr_err("%s: Invalid session %d receivced expected %d\n",
  159. __func__, token, client->session);
  160. return -EINVAL;
  161. }
  162. client->cmd_err_code = payload[1];
  163. if (client->cmd_err_code)
  164. pr_err("%s: cmd 0x%x failed status %d\n",
  165. __func__, payload[0], client->cmd_err_code);
  166. if (atomic_cmpxchg(&client->cmd_state,
  167. CMD_STATE_WAIT_RESP,
  168. CMD_STATE_CLEARED) ==
  169. CMD_STATE_WAIT_RESP)
  170. wake_up(&client->cmd_wait);
  171. break;
  172. default:
  173. pr_debug("%s: Unknown command 0x%x\n",
  174. __func__, payload[0]);
  175. break;
  176. }
  177. return 0;
  178. }
  179. if (client->cb)
  180. client->cb(data->opcode, data->token, data->payload,
  181. client->priv);
  182. return 0;
  183. }
  184. static int q6lsm_session_alloc(struct lsm_client *client)
  185. {
  186. unsigned long flags;
  187. int n, ret = -ENOMEM;
  188. spin_lock_irqsave(&lsm_session_lock, flags);
  189. for (n = LSM_MIN_SESSION_ID; n <= LSM_MAX_SESSION_ID; n++) {
  190. if (!lsm_session[n]) {
  191. lsm_session[n] = client;
  192. ret = n;
  193. break;
  194. }
  195. }
  196. spin_unlock_irqrestore(&lsm_session_lock, flags);
  197. pr_debug("%s: Alloc Session %d", __func__, n);
  198. return ret;
  199. }
  200. static void q6lsm_session_free(struct lsm_client *client)
  201. {
  202. unsigned long flags;
  203. pr_debug("%s: Freeing session ID %d\n", __func__, client->session);
  204. spin_lock_irqsave(&lsm_session_lock, flags);
  205. lsm_session[client->session] = LSM_INVALID_SESSION_ID;
  206. spin_unlock_irqrestore(&lsm_session_lock, flags);
  207. client->session = LSM_INVALID_SESSION_ID;
  208. }
  209. static void *q6lsm_mmap_apr_reg(void)
  210. {
  211. if (atomic_inc_return(&lsm_common.apr_users) == 1) {
  212. lsm_common.apr =
  213. apr_register("ADSP", "LSM", q6lsm_mmapcallback,
  214. 0x0FFFFFFFF, &lsm_common);
  215. if (!lsm_common.apr) {
  216. pr_debug("%s: Unable to register APR LSM common port\n",
  217. __func__);
  218. atomic_dec(&lsm_common.apr_users);
  219. }
  220. }
  221. return lsm_common.apr;
  222. }
  223. static int q6lsm_mmap_apr_dereg(void)
  224. {
  225. if (atomic_read(&lsm_common.apr_users) <= 0) {
  226. WARN("%s: APR common port already closed\n", __func__);
  227. } else {
  228. if (atomic_dec_return(&lsm_common.apr_users) == 0) {
  229. apr_deregister(lsm_common.apr);
  230. pr_debug("%s: APR De-Register common port\n", __func__);
  231. }
  232. }
  233. return 0;
  234. }
  235. /**
  236. * q6lsm_client_alloc -
  237. * Allocate session for LSM client
  238. *
  239. * @cb: callback fn
  240. * @priv: private data
  241. *
  242. * Returns LSM client handle on success or NULL on failure
  243. */
  244. struct lsm_client *q6lsm_client_alloc(lsm_app_cb cb, void *priv)
  245. {
  246. struct lsm_client *client;
  247. int n;
  248. client = kzalloc(sizeof(struct lsm_client), GFP_KERNEL);
  249. if (!client)
  250. return NULL;
  251. n = q6lsm_session_alloc(client);
  252. if (n <= 0) {
  253. kfree(client);
  254. return NULL;
  255. }
  256. client->session = n;
  257. client->cb = cb;
  258. client->priv = priv;
  259. if (CHECK_SESSION(client->session)) {
  260. pr_err("%s: Client session %d\n",
  261. __func__, client->session);
  262. kfree(client);
  263. return NULL;
  264. }
  265. pr_debug("%s: Client Session %d\n", __func__, client->session);
  266. client->apr = apr_register("ADSP", "LSM", q6lsm_callback,
  267. ((client->session) << 8 | client->session),
  268. client);
  269. if (client->apr == NULL) {
  270. pr_err("%s: Registration with APR failed\n", __func__);
  271. goto fail;
  272. }
  273. pr_debug("%s: Registering the common port with APR\n", __func__);
  274. client->mmap_apr = q6lsm_mmap_apr_reg();
  275. if (!client->mmap_apr) {
  276. pr_err("%s: APR registration failed\n", __func__);
  277. goto fail;
  278. }
  279. init_waitqueue_head(&client->cmd_wait);
  280. mutex_init(&client->cmd_lock);
  281. atomic_set(&client->cmd_state, CMD_STATE_CLEARED);
  282. pr_debug("%s: New client allocated\n", __func__);
  283. return client;
  284. fail:
  285. q6lsm_client_free(client);
  286. return NULL;
  287. }
  288. EXPORT_SYMBOL(q6lsm_client_alloc);
  289. /**
  290. * q6lsm_client_free -
  291. * Performs LSM client free
  292. *
  293. * @client: LSM client handle
  294. *
  295. */
  296. void q6lsm_client_free(struct lsm_client *client)
  297. {
  298. if (!client)
  299. return;
  300. if (CHECK_SESSION(client->session)) {
  301. pr_err("%s: Invalid Session %d\n", __func__, client->session);
  302. return;
  303. }
  304. apr_deregister(client->apr);
  305. client->mmap_apr = NULL;
  306. q6lsm_session_free(client);
  307. q6lsm_mmap_apr_dereg();
  308. mutex_destroy(&client->cmd_lock);
  309. kfree(client);
  310. client = NULL;
  311. }
  312. EXPORT_SYMBOL(q6lsm_client_free);
  313. /*
  314. * q6lsm_apr_send_pkt : If wait == true, hold mutex to prevent from preempting
  315. * other thread's wait.
  316. * If mmap_handle_p != NULL, disable irq and spin lock to
  317. * protect mmap_handle_p
  318. */
  319. static int q6lsm_apr_send_pkt(struct lsm_client *client, void *handle,
  320. void *data, bool wait, uint32_t *mmap_p)
  321. {
  322. int ret;
  323. unsigned long flags = 0;
  324. struct apr_hdr *msg_hdr = (struct apr_hdr *) data;
  325. if (!handle) {
  326. pr_err("%s: handle is NULL\n", __func__);
  327. return -EINVAL;
  328. }
  329. pr_debug("%s: enter wait %d\n", __func__, wait);
  330. if (wait)
  331. mutex_lock(&lsm_common.apr_lock);
  332. if (mmap_p) {
  333. WARN_ON(!wait);
  334. spin_lock_irqsave(&mmap_lock, flags);
  335. mmap_handle_p = mmap_p;
  336. }
  337. atomic_set(&client->cmd_state, CMD_STATE_WAIT_RESP);
  338. client->cmd_err_code = 0;
  339. ret = apr_send_pkt(handle, data);
  340. if (mmap_p)
  341. spin_unlock_irqrestore(&mmap_lock, flags);
  342. if (ret < 0) {
  343. pr_err("%s: apr_send_pkt failed %d\n", __func__, ret);
  344. } else if (wait) {
  345. ret = wait_event_timeout(client->cmd_wait,
  346. (atomic_read(&client->cmd_state) ==
  347. CMD_STATE_CLEARED),
  348. APR_TIMEOUT);
  349. if (likely(ret)) {
  350. /* q6 returned error */
  351. if (client->cmd_err_code) {
  352. pr_err("%s: DSP returned error[%s]\n",
  353. __func__, adsp_err_get_err_str(
  354. client->cmd_err_code));
  355. ret = adsp_err_get_lnx_err_code(
  356. client->cmd_err_code);
  357. } else {
  358. ret = 0;
  359. }
  360. } else {
  361. pr_err("%s: wait timedout, apr_opcode = 0x%x, size = %d\n",
  362. __func__, msg_hdr->opcode, msg_hdr->pkt_size);
  363. /* ret = 0 means wait timed out */
  364. ret = -ETIMEDOUT;
  365. }
  366. } else {
  367. ret = 0;
  368. }
  369. if (wait)
  370. mutex_unlock(&lsm_common.apr_lock);
  371. pr_debug("%s: leave ret %d\n", __func__, ret);
  372. return ret;
  373. }
  374. static void q6lsm_add_hdr(struct lsm_client *client, struct apr_hdr *hdr,
  375. uint32_t pkt_size, bool cmd_flg)
  376. {
  377. pr_debug("%s: pkt_size %d cmd_flg %d session %d\n", __func__,
  378. pkt_size, cmd_flg, client->session);
  379. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  380. APR_HDR_LEN(sizeof(struct apr_hdr)),
  381. APR_PKT_VER);
  382. hdr->src_svc = APR_SVC_LSM;
  383. hdr->src_domain = APR_DOMAIN_APPS;
  384. hdr->dest_svc = APR_SVC_LSM;
  385. hdr->dest_domain = APR_DOMAIN_ADSP;
  386. hdr->src_port = ((client->session << 8) & 0xFF00) | client->session;
  387. hdr->dest_port = ((client->session << 8) & 0xFF00) | client->session;
  388. hdr->pkt_size = pkt_size;
  389. if (cmd_flg)
  390. hdr->token = client->session;
  391. }
  392. static int q6lsm_pack_params(u8 *dest, struct param_hdr_v3 *param_info,
  393. u8 *param_data, size_t *final_length,
  394. u32 set_param_opcode)
  395. {
  396. bool iid_supported = q6common_is_instance_id_supported();
  397. union param_hdrs *param_hdr = NULL;
  398. u32 param_size = param_info->param_size;
  399. size_t hdr_size;
  400. size_t provided_size = *final_length;
  401. hdr_size = iid_supported ? sizeof(struct param_hdr_v3) :
  402. sizeof(struct param_hdr_v2);
  403. if (provided_size < hdr_size) {
  404. pr_err("%s: Provided size %zu is not large enough, need %zu\n",
  405. __func__, provided_size, hdr_size);
  406. return -EINVAL;
  407. }
  408. if (iid_supported) {
  409. memcpy(dest, param_info, hdr_size);
  410. } else {
  411. /* MID, PID and structure size are the same in V1 and V2 */
  412. param_hdr = (union param_hdrs *) dest;
  413. param_hdr->v2.module_id = param_info->module_id;
  414. param_hdr->v2.param_id = param_info->param_id;
  415. switch (set_param_opcode) {
  416. case LSM_SESSION_CMD_SET_PARAMS_V2:
  417. param_hdr->v2.param_size = param_size;
  418. break;
  419. case LSM_SESSION_CMD_SET_PARAMS:
  420. default:
  421. if (param_size > U16_MAX) {
  422. pr_err("%s: Invalid param size %d\n", __func__,
  423. param_size);
  424. return -EINVAL;
  425. }
  426. param_hdr->v1.param_size = param_size;
  427. param_hdr->v1.reserved = 0;
  428. break;
  429. }
  430. }
  431. *final_length = hdr_size;
  432. if (param_data != NULL) {
  433. if (provided_size < hdr_size + param_size) {
  434. pr_err("%s: Provided size %zu is not large enough, need %zu\n",
  435. __func__, provided_size, hdr_size + param_size);
  436. return -EINVAL;
  437. }
  438. memcpy(dest + hdr_size, param_data, param_size);
  439. *final_length += param_size;
  440. }
  441. return 0;
  442. }
  443. static int q6lsm_set_params_v2(struct lsm_client *client,
  444. struct mem_mapping_hdr *mem_hdr,
  445. uint8_t *param_data, uint32_t param_size,
  446. uint32_t set_param_opcode)
  447. {
  448. struct lsm_session_cmd_set_params_v2 *lsm_set_param = NULL;
  449. uint32_t pkt_size = 0;
  450. int ret;
  451. pkt_size = sizeof(struct lsm_session_cmd_set_params_v2);
  452. /* Only include param size in packet size when inband */
  453. if (param_data != NULL)
  454. pkt_size += param_size;
  455. lsm_set_param = kzalloc(pkt_size, GFP_KERNEL);
  456. if (!lsm_set_param)
  457. return -ENOMEM;
  458. q6lsm_add_hdr(client, &lsm_set_param->apr_hdr, pkt_size, true);
  459. lsm_set_param->apr_hdr.opcode = set_param_opcode;
  460. lsm_set_param->payload_size = param_size;
  461. if (mem_hdr != NULL) {
  462. lsm_set_param->mem_hdr = *mem_hdr;
  463. } else if (param_data != NULL) {
  464. memcpy(lsm_set_param->param_data, param_data, param_size);
  465. } else {
  466. pr_err("%s: Received NULL pointers for both memory header and data\n",
  467. __func__);
  468. ret = -EINVAL;
  469. goto done;
  470. }
  471. ret = q6lsm_apr_send_pkt(client, client->apr, lsm_set_param, true,
  472. NULL);
  473. done:
  474. kfree(lsm_set_param);
  475. return ret;
  476. }
  477. static int q6lsm_set_params_v3(struct lsm_client *client,
  478. struct mem_mapping_hdr *mem_hdr,
  479. uint8_t *param_data, uint32_t param_size)
  480. {
  481. struct lsm_session_cmd_set_params_v3 *lsm_set_param = NULL;
  482. uint16_t pkt_size = 0;
  483. int ret = 0;
  484. pkt_size = sizeof(struct lsm_session_cmd_set_params_v3);
  485. /* Only include param size in packet size when inband */
  486. if (param_data != NULL)
  487. pkt_size += param_size;
  488. lsm_set_param = kzalloc(pkt_size, GFP_KERNEL);
  489. if (!lsm_set_param)
  490. return -ENOMEM;
  491. q6lsm_add_hdr(client, &lsm_set_param->apr_hdr, pkt_size, true);
  492. lsm_set_param->apr_hdr.opcode = LSM_SESSION_CMD_SET_PARAMS_V3;
  493. lsm_set_param->payload_size = param_size;
  494. if (mem_hdr != NULL) {
  495. lsm_set_param->mem_hdr = *mem_hdr;
  496. } else if (param_data != NULL) {
  497. memcpy(lsm_set_param->param_data, param_data, param_size);
  498. } else {
  499. pr_err("%s: Received NULL pointers for both memory header and data\n",
  500. __func__);
  501. ret = -EINVAL;
  502. goto done;
  503. }
  504. ret = q6lsm_apr_send_pkt(client, client->apr, lsm_set_param, true,
  505. NULL);
  506. done:
  507. kfree(lsm_set_param);
  508. return ret;
  509. }
  510. static int q6lsm_set_params(struct lsm_client *client,
  511. struct mem_mapping_hdr *mem_hdr,
  512. uint8_t *param_data, uint32_t param_size,
  513. uint32_t set_param_opcode)
  514. {
  515. if (q6common_is_instance_id_supported())
  516. return q6lsm_set_params_v3(client, mem_hdr, param_data,
  517. param_size);
  518. else
  519. return q6lsm_set_params_v2(client, mem_hdr, param_data,
  520. param_size, set_param_opcode);
  521. }
  522. static int q6lsm_pack_and_set_params(struct lsm_client *client,
  523. struct param_hdr_v3 *param_info,
  524. uint8_t *param_data,
  525. uint32_t set_param_opcode)
  526. {
  527. u8 *packed_data = NULL;
  528. size_t total_size = 0;
  529. int ret = 0;
  530. total_size = sizeof(union param_hdrs) + param_info->param_size;
  531. packed_data = kzalloc(total_size, GFP_KERNEL);
  532. if (!packed_data)
  533. return -ENOMEM;
  534. ret = q6lsm_pack_params(packed_data, param_info, param_data,
  535. &total_size, set_param_opcode);
  536. if (ret)
  537. goto done;
  538. ret = q6lsm_set_params(client, NULL, packed_data, total_size,
  539. set_param_opcode);
  540. done:
  541. kfree(packed_data);
  542. return ret;
  543. }
  544. static int q6lsm_send_custom_topologies(struct lsm_client *client)
  545. {
  546. int rc;
  547. struct cal_block_data *cal_block = NULL;
  548. struct lsm_custom_topologies cstm_top;
  549. if (lsm_common.cal_data[LSM_CUSTOM_TOP_IDX] == NULL) {
  550. pr_err("%s: LSM_CUSTOM_TOP_IDX invalid\n", __func__);
  551. rc = -EINVAL;
  552. goto done;
  553. }
  554. lsm_common.set_custom_topology = 0;
  555. mutex_lock(&lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]->lock);
  556. cal_block = cal_utils_get_only_cal_block(
  557. lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]);
  558. if (!cal_block) {
  559. pr_err("%s: Cal block for LSM_CUSTOM_TOP_IDX not found\n",
  560. __func__);
  561. rc = -EINVAL;
  562. goto unlock;
  563. }
  564. if (cal_block->cal_data.size <= 0) {
  565. pr_err("%s: Invalid size for LSM_CUSTOM_TOP %zd\n",
  566. __func__, cal_block->cal_data.size);
  567. rc = -EINVAL;
  568. goto unlock;
  569. }
  570. memset(&cstm_top, 0, sizeof(cstm_top));
  571. /* Map the memory for out-of-band data */
  572. rc = q6lsm_memory_map_regions(client, cal_block->cal_data.paddr,
  573. cal_block->map_data.map_size,
  574. &cal_block->map_data.q6map_handle);
  575. if (rc < 0) {
  576. pr_err("%s: Failed to map custom topologied, err = %d\n",
  577. __func__, rc);
  578. goto unlock;
  579. }
  580. q6lsm_add_hdr(client, &cstm_top.hdr,
  581. sizeof(cstm_top), true);
  582. cstm_top.hdr.opcode = LSM_CMD_ADD_TOPOLOGIES;
  583. /*
  584. * For ADD_TOPOLOGIES, the dest_port should be 0
  585. * Note that source port cannot be zero as it is used
  586. * to route the response to a specific client registered
  587. * on APR
  588. */
  589. cstm_top.hdr.dest_port = 0;
  590. cstm_top.data_payload_addr_lsw =
  591. lower_32_bits(cal_block->cal_data.paddr);
  592. cstm_top.data_payload_addr_msw =
  593. msm_audio_populate_upper_32_bits(
  594. cal_block->cal_data.paddr);
  595. cstm_top.mem_map_handle = cal_block->map_data.q6map_handle;
  596. cstm_top.buffer_size = cal_block->cal_data.size;
  597. rc = q6lsm_apr_send_pkt(client, client->apr,
  598. &cstm_top, true, NULL);
  599. if (rc)
  600. pr_err("%s: Failed to add custom top, err = %d\n",
  601. __func__, rc);
  602. /* go ahead and unmap even if custom top failed */
  603. rc = q6lsm_memory_unmap_regions(client,
  604. cal_block->map_data.q6map_handle);
  605. if (rc) {
  606. pr_err("%s: Failed to unmap, err = %d\n",
  607. __func__, rc);
  608. /* Even if mem unmap failed, treat the cmd as success */
  609. rc = 0;
  610. }
  611. unlock:
  612. mutex_unlock(&lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]->lock);
  613. done:
  614. return rc;
  615. }
  616. static int q6lsm_do_open_v2(struct lsm_client *client,
  617. uint16_t app_id)
  618. {
  619. int rc;
  620. struct cal_block_data *cal_block = NULL;
  621. struct audio_cal_info_lsm_top *lsm_top;
  622. struct lsm_stream_cmd_open_tx_v2 open_v2;
  623. if (lsm_common.cal_data[LSM_TOP_IDX] == NULL) {
  624. pr_err("%s: LSM_TOP_IDX invalid\n", __func__);
  625. rc = -EINVAL;
  626. goto done;
  627. }
  628. mutex_lock(&lsm_common.cal_data[LSM_TOP_IDX]->lock);
  629. cal_block = cal_utils_get_only_cal_block(
  630. lsm_common.cal_data[LSM_TOP_IDX]);
  631. if (!cal_block) {
  632. pr_err("%s: Cal block for LSM_TOP_IDX not found\n",
  633. __func__);
  634. rc = -EINVAL;
  635. goto unlock;
  636. }
  637. lsm_top = (struct audio_cal_info_lsm_top *)
  638. cal_block->cal_info;
  639. if (!lsm_top) {
  640. pr_err("%s: cal_info for LSM_TOP_IDX not found\n",
  641. __func__);
  642. rc = -EINVAL;
  643. goto unlock;
  644. }
  645. pr_debug("%s: topology_id = 0x%x, acdb_id = 0x%x, app_type = 0x%x\n",
  646. __func__, lsm_top->topology, lsm_top->acdb_id,
  647. lsm_top->app_type);
  648. if (lsm_top->topology == 0) {
  649. pr_err("%s: toplogy id not sent for app_type 0x%x\n",
  650. __func__, lsm_top->app_type);
  651. rc = -EINVAL;
  652. goto unlock;
  653. }
  654. client->app_id = lsm_top->app_type;
  655. memset(&open_v2, 0, sizeof(open_v2));
  656. q6lsm_add_hdr(client, &open_v2.hdr,
  657. sizeof(open_v2), true);
  658. open_v2.topology_id = lsm_top->topology;
  659. open_v2.hdr.opcode = LSM_SESSION_CMD_OPEN_TX_V2;
  660. rc = q6lsm_apr_send_pkt(client, client->apr,
  661. &open_v2, true, NULL);
  662. if (rc)
  663. pr_err("%s: open_v2 failed, err = %d\n",
  664. __func__, rc);
  665. else
  666. client->use_topology = true;
  667. unlock:
  668. mutex_unlock(&lsm_common.cal_data[LSM_TOP_IDX]->lock);
  669. done:
  670. return rc;
  671. }
  672. /**
  673. * q6lsm_sm_set_param_data -
  674. * Update sound model param data
  675. *
  676. * @client: LSM client handle
  677. * @p_info: param info
  678. * @offset: pointer to retrieve size
  679. *
  680. */
  681. void q6lsm_sm_set_param_data(struct lsm_client *client,
  682. struct lsm_params_info *p_info,
  683. size_t *offset)
  684. {
  685. struct param_hdr_v3 param_hdr;
  686. int ret = 0;
  687. memset(&param_hdr, 0, sizeof(param_hdr));
  688. param_hdr.module_id = p_info->module_id;
  689. param_hdr.instance_id = INSTANCE_ID_0;
  690. param_hdr.param_id = p_info->param_id;
  691. param_hdr.param_size = client->sound_model.size;
  692. ret = q6lsm_pack_params(client->sound_model.data, &param_hdr,
  693. NULL, offset, LSM_SESSION_CMD_SET_PARAMS_V2);
  694. if (ret)
  695. pr_err("%s: Failed to pack params, error %d\n", __func__, ret);
  696. }
  697. EXPORT_SYMBOL(q6lsm_sm_set_param_data);
  698. /**
  699. * q6lsm_open -
  700. * command to open LSM session
  701. *
  702. * @client: LSM client handle
  703. * @app_id: App ID for LSM
  704. *
  705. * Returns 0 on success or error on failure
  706. */
  707. int q6lsm_open(struct lsm_client *client, uint16_t app_id)
  708. {
  709. int rc = 0;
  710. struct lsm_stream_cmd_open_tx open;
  711. /* Add Custom topologies if needed */
  712. if (lsm_common.set_custom_topology) {
  713. rc = q6lsm_send_custom_topologies(client);
  714. if (rc)
  715. pr_err("%s: Failed to send cust_top, err = %d\n",
  716. __func__, rc);
  717. }
  718. /* Try to open with topology first */
  719. rc = q6lsm_do_open_v2(client, app_id);
  720. if (!rc)
  721. /* open_v2 was successful */
  722. goto done;
  723. pr_debug("%s: try without topology\n",
  724. __func__);
  725. memset(&open, 0, sizeof(open));
  726. q6lsm_add_hdr(client, &open.hdr, sizeof(open), true);
  727. switch (client->app_id) {
  728. case LSM_VOICE_WAKEUP_APP_ID_V2:
  729. open.app_id = client->app_id;
  730. break;
  731. default:
  732. pr_err("%s: default err 0x%x\n", __func__, client->app_id);
  733. rc = -EINVAL;
  734. break;
  735. }
  736. open.sampling_rate = LSM_SAMPLE_RATE;
  737. open.hdr.opcode = LSM_SESSION_CMD_OPEN_TX;
  738. rc = q6lsm_apr_send_pkt(client, client->apr,
  739. &open, true, NULL);
  740. if (rc)
  741. pr_err("%s: Open failed opcode 0x%x, rc %d\n",
  742. __func__, open.hdr.opcode, rc);
  743. else
  744. client->use_topology = false;
  745. done:
  746. pr_debug("%s: leave %d\n", __func__, rc);
  747. return rc;
  748. }
  749. EXPORT_SYMBOL(q6lsm_open);
  750. static int q6lsm_send_confidence_levels(struct lsm_client *client,
  751. struct param_hdr_v3 *param_info,
  752. uint32_t set_param_opcode)
  753. {
  754. struct lsm_param_confidence_levels *conf_levels = NULL;
  755. uint32_t num_conf_levels = client->num_confidence_levels;
  756. uint8_t i = 0;
  757. uint8_t padd_size = 0;
  758. uint32_t param_size = 0;
  759. int rc = 0;
  760. /* Data must be 4 byte aligned so add any necessary padding. */
  761. padd_size = (4 - (num_conf_levels % 4)) - 1;
  762. param_size = (sizeof(uint8_t) + num_conf_levels + padd_size) *
  763. sizeof(uint8_t);
  764. param_info->param_size = param_size;
  765. pr_debug("%s: Set Conf Levels PARAM SIZE = %d\n", __func__, param_size);
  766. conf_levels = kzalloc(param_size, GFP_KERNEL);
  767. if (!conf_levels)
  768. return -ENOMEM;
  769. conf_levels->num_confidence_levels = num_conf_levels;
  770. pr_debug("%s: Num conf_level = %d\n", __func__, num_conf_levels);
  771. memcpy(conf_levels->confidence_levels, client->confidence_levels,
  772. num_conf_levels);
  773. for (i = 0; i < num_conf_levels; i++)
  774. pr_debug("%s: Confidence_level[%d] = %d\n", __func__, i,
  775. conf_levels->confidence_levels[i]);
  776. rc = q6lsm_pack_and_set_params(client, param_info,
  777. (uint8_t *) conf_levels,
  778. set_param_opcode);
  779. if (rc)
  780. pr_err("%s: Send confidence_levels cmd failed, err = %d\n",
  781. __func__, rc);
  782. kfree(conf_levels);
  783. return rc;
  784. }
  785. static int q6lsm_send_param_opmode(struct lsm_client *client,
  786. struct param_hdr_v3 *param_info,
  787. u32 set_param_opcode)
  788. {
  789. struct lsm_param_op_mode op_mode;
  790. int rc = 0;
  791. memset(&op_mode, 0, sizeof(op_mode));
  792. param_info->param_size = sizeof(op_mode);
  793. op_mode.minor_version = QLSM_PARAM_ID_MINOR_VERSION;
  794. op_mode.mode = client->mode;
  795. pr_debug("%s: mode = 0x%x", __func__, op_mode.mode);
  796. rc = q6lsm_pack_and_set_params(client, param_info, (uint8_t *) &op_mode,
  797. set_param_opcode);
  798. if (rc)
  799. pr_err("%s: Failed set_params, rc %d\n", __func__, rc);
  800. pr_debug("%s: leave %d\n", __func__, rc);
  801. return rc;
  802. }
  803. /**
  804. * set_lsm_port -
  805. * Update LSM AFE port
  806. *
  807. */
  808. void set_lsm_port(int lsm_port)
  809. {
  810. lsm_afe_port = lsm_port;
  811. }
  812. EXPORT_SYMBOL(set_lsm_port);
  813. int get_lsm_port(void)
  814. {
  815. return lsm_afe_port;
  816. }
  817. /**
  818. * q6lsm_set_port_connected -
  819. * command to set LSM port connected
  820. *
  821. * @client: LSM client handle
  822. *
  823. * Returns 0 on success or error on failure
  824. */
  825. int q6lsm_set_port_connected(struct lsm_client *client)
  826. {
  827. struct lsm_param_connect_to_port connect_port;
  828. struct param_hdr_v3 connectport_hdr;
  829. u32 set_param_opcode = 0;
  830. int rc = 0;
  831. memset(&connect_port, 0, sizeof(connect_port));
  832. memset(&connectport_hdr, 0, sizeof(connectport_hdr));
  833. if (client->use_topology) {
  834. set_param_opcode = LSM_SESSION_CMD_SET_PARAMS_V2;
  835. connectport_hdr.module_id = LSM_MODULE_ID_FRAMEWORK;
  836. } else {
  837. set_param_opcode = LSM_SESSION_CMD_SET_PARAMS;
  838. connectport_hdr.module_id = LSM_MODULE_ID_VOICE_WAKEUP;
  839. }
  840. connectport_hdr.instance_id = INSTANCE_ID_0;
  841. connectport_hdr.param_id = LSM_PARAM_ID_CONNECT_TO_PORT;
  842. connectport_hdr.param_size = sizeof(connect_port);
  843. client->connect_to_port = get_lsm_port();
  844. connect_port.minor_version = QLSM_PARAM_ID_MINOR_VERSION;
  845. connect_port.port_id = client->connect_to_port;
  846. rc = q6lsm_pack_and_set_params(client, &connectport_hdr,
  847. (uint8_t *) &connect_port,
  848. set_param_opcode);
  849. if (rc)
  850. pr_err("%s: Failed set_params, rc %d\n", __func__, rc);
  851. return rc;
  852. }
  853. EXPORT_SYMBOL(q6lsm_set_port_connected);
  854. static int q6lsm_send_param_polling_enable(struct lsm_client *client,
  855. bool poll_en,
  856. struct param_hdr_v3 *param_info,
  857. u32 set_param_opcode)
  858. {
  859. struct lsm_param_poll_enable polling_enable;
  860. int rc = 0;
  861. memset(&polling_enable, 0, sizeof(polling_enable));
  862. param_info->param_size = sizeof(polling_enable);
  863. polling_enable.minor_version = QLSM_PARAM_ID_MINOR_VERSION;
  864. polling_enable.polling_enable = (poll_en) ? 1 : 0;
  865. rc = q6lsm_pack_and_set_params(client, param_info,
  866. (uint8_t *) &polling_enable,
  867. set_param_opcode);
  868. if (rc)
  869. pr_err("%s: Failed set_params, rc %d\n", __func__, rc);
  870. return rc;
  871. }
  872. /**
  873. * q6lsm_set_fwk_mode_cfg -
  874. * command to set LSM fwk mode cfg
  875. *
  876. * @client: LSM client handle
  877. * @event_mode: mode for fwk cfg
  878. *
  879. * Returns 0 on success or error on failure
  880. */
  881. int q6lsm_set_fwk_mode_cfg(struct lsm_client *client,
  882. uint32_t event_mode)
  883. {
  884. struct lsm_param_fwk_mode_cfg fwk_mode_cfg;
  885. struct param_hdr_v3 fwk_mode_cfg_hdr;
  886. int rc = 0;
  887. memset(&fwk_mode_cfg, 0, sizeof(fwk_mode_cfg));
  888. memset(&fwk_mode_cfg_hdr, 0, sizeof(fwk_mode_cfg_hdr));
  889. if (!client->use_topology) {
  890. pr_debug("%s: Ignore sending event mode\n", __func__);
  891. return rc;
  892. }
  893. fwk_mode_cfg_hdr.module_id = LSM_MODULE_ID_FRAMEWORK;
  894. fwk_mode_cfg_hdr.instance_id = INSTANCE_ID_0;
  895. fwk_mode_cfg_hdr.param_id = LSM_PARAM_ID_FWK_MODE_CONFIG;
  896. fwk_mode_cfg_hdr.param_size = sizeof(fwk_mode_cfg);
  897. fwk_mode_cfg.minor_version = QLSM_PARAM_ID_MINOR_VERSION;
  898. fwk_mode_cfg.mode = event_mode;
  899. pr_debug("%s: mode = %d\n", __func__, fwk_mode_cfg.mode);
  900. rc = q6lsm_pack_and_set_params(client, &fwk_mode_cfg_hdr,
  901. (uint8_t *) &fwk_mode_cfg,
  902. LSM_SESSION_CMD_SET_PARAMS_V2);
  903. if (rc)
  904. pr_err("%s: Failed set_params, rc %d\n", __func__, rc);
  905. return rc;
  906. }
  907. EXPORT_SYMBOL(q6lsm_set_fwk_mode_cfg);
  908. static int q6lsm_arrange_mch_map(struct lsm_param_media_fmt *media_fmt,
  909. int channel_count)
  910. {
  911. int rc = 0;
  912. memset(media_fmt->channel_mapping, 0, LSM_MAX_NUM_CHANNELS);
  913. switch (channel_count) {
  914. case 1:
  915. media_fmt->channel_mapping[0] = PCM_CHANNEL_FC;
  916. break;
  917. case 2:
  918. media_fmt->channel_mapping[0] = PCM_CHANNEL_FL;
  919. media_fmt->channel_mapping[1] = PCM_CHANNEL_FR;
  920. break;
  921. case 3:
  922. media_fmt->channel_mapping[0] = PCM_CHANNEL_FL;
  923. media_fmt->channel_mapping[1] = PCM_CHANNEL_FR;
  924. media_fmt->channel_mapping[2] = PCM_CHANNEL_FC;
  925. break;
  926. case 4:
  927. media_fmt->channel_mapping[0] = PCM_CHANNEL_FL;
  928. media_fmt->channel_mapping[1] = PCM_CHANNEL_FR;
  929. media_fmt->channel_mapping[2] = PCM_CHANNEL_LS;
  930. media_fmt->channel_mapping[3] = PCM_CHANNEL_RS;
  931. break;
  932. default:
  933. pr_err("%s: invalid num_chan %d\n", __func__, channel_count);
  934. rc = -EINVAL;
  935. break;
  936. }
  937. return rc;
  938. }
  939. /**
  940. * q6lsm_set_media_fmt_params -
  941. * command to set LSM media fmt params
  942. *
  943. * @client: LSM client handle
  944. *
  945. * Returns 0 on success or error on failure
  946. */
  947. int q6lsm_set_media_fmt_params(struct lsm_client *client)
  948. {
  949. struct lsm_param_media_fmt media_fmt;
  950. struct lsm_hw_params param = client->hw_params;
  951. struct param_hdr_v3 media_fmt_hdr;
  952. int rc = 0;
  953. memset(&media_fmt, 0, sizeof(media_fmt));
  954. memset(&media_fmt_hdr, 0, sizeof(media_fmt_hdr));
  955. if (!client->use_topology) {
  956. pr_debug("%s: Ignore sending media format\n", __func__);
  957. goto err_ret;
  958. }
  959. media_fmt_hdr.module_id = LSM_MODULE_ID_FRAMEWORK;
  960. media_fmt_hdr.instance_id = INSTANCE_ID_0;
  961. media_fmt_hdr.param_id = LSM_PARAM_ID_MEDIA_FMT;
  962. media_fmt_hdr.param_size = sizeof(media_fmt);
  963. media_fmt.minor_version = QLSM_PARAM_ID_MINOR_VERSION_2;
  964. media_fmt.sample_rate = param.sample_rate;
  965. media_fmt.num_channels = param.num_chs;
  966. media_fmt.bit_width = param.sample_size;
  967. rc = q6lsm_arrange_mch_map(&media_fmt, media_fmt.num_channels);
  968. if (rc)
  969. goto err_ret;
  970. pr_debug("%s: sample rate= %d, channels %d bit width %d\n", __func__,
  971. media_fmt.sample_rate, media_fmt.num_channels,
  972. media_fmt.bit_width);
  973. rc = q6lsm_pack_and_set_params(client, &media_fmt_hdr,
  974. (uint8_t *) &media_fmt,
  975. LSM_SESSION_CMD_SET_PARAMS_V2);
  976. if (rc)
  977. pr_err("%s: Failed set_params, rc %d\n", __func__, rc);
  978. err_ret:
  979. return rc;
  980. }
  981. EXPORT_SYMBOL(q6lsm_set_media_fmt_params);
  982. /**
  983. * q6lsm_set_data -
  984. * Command to set LSM data
  985. *
  986. * @client: LSM client handle
  987. * @mode: LSM detection mode value
  988. * @detectfailure: flag for detect failure
  989. *
  990. * Returns 0 on success or error on failure
  991. */
  992. int q6lsm_set_data(struct lsm_client *client,
  993. enum lsm_detection_mode mode,
  994. bool detectfailure)
  995. {
  996. struct param_hdr_v3 param_hdr;
  997. int rc = 0;
  998. memset(&param_hdr, 0, sizeof(param_hdr));
  999. if (!client->confidence_levels) {
  1000. /*
  1001. * It is possible that confidence levels are
  1002. * not provided. This is not a error condition.
  1003. * Return gracefully without any error
  1004. */
  1005. pr_debug("%s: no conf levels to set\n",
  1006. __func__);
  1007. return rc;
  1008. }
  1009. if (mode == LSM_MODE_KEYWORD_ONLY_DETECTION) {
  1010. client->mode = 0x01;
  1011. } else if (mode == LSM_MODE_USER_KEYWORD_DETECTION) {
  1012. client->mode = 0x03;
  1013. } else {
  1014. pr_err("%s: Incorrect detection mode %d\n", __func__, mode);
  1015. rc = -EINVAL;
  1016. goto err_ret;
  1017. }
  1018. client->mode |= detectfailure << 2;
  1019. param_hdr.module_id = LSM_MODULE_ID_VOICE_WAKEUP;
  1020. param_hdr.instance_id = INSTANCE_ID_0;
  1021. param_hdr.param_id = LSM_PARAM_ID_OPERATION_MODE;
  1022. rc = q6lsm_send_param_opmode(client, &param_hdr,
  1023. LSM_SESSION_CMD_SET_PARAMS);
  1024. if (rc) {
  1025. pr_err("%s: Failed to set lsm config params %d\n",
  1026. __func__, rc);
  1027. goto err_ret;
  1028. }
  1029. param_hdr.param_id = LSM_PARAM_ID_MIN_CONFIDENCE_LEVELS;
  1030. rc = q6lsm_send_confidence_levels(client, &param_hdr,
  1031. LSM_SESSION_CMD_SET_PARAMS);
  1032. if (rc) {
  1033. pr_err("%s: Failed to send conf_levels, err = %d\n",
  1034. __func__, rc);
  1035. goto err_ret;
  1036. }
  1037. rc = q6lsm_send_cal(client, LSM_SESSION_CMD_SET_PARAMS);
  1038. if (rc) {
  1039. pr_err("%s: Failed to send calibration data %d\n",
  1040. __func__, rc);
  1041. goto err_ret;
  1042. }
  1043. err_ret:
  1044. return rc;
  1045. }
  1046. EXPORT_SYMBOL(q6lsm_set_data);
  1047. /**
  1048. * q6lsm_register_sound_model -
  1049. * Register LSM snd model
  1050. *
  1051. * @client: LSM client handle
  1052. *
  1053. * Returns 0 on success or error on failure
  1054. */
  1055. int q6lsm_register_sound_model(struct lsm_client *client,
  1056. enum lsm_detection_mode mode,
  1057. bool detectfailure)
  1058. {
  1059. int rc;
  1060. struct lsm_cmd_reg_snd_model cmd;
  1061. memset(&cmd, 0, sizeof(cmd));
  1062. rc = q6lsm_set_data(client, mode, detectfailure);
  1063. if (rc) {
  1064. pr_err("%s: Failed to set lsm data, err = %d\n",
  1065. __func__, rc);
  1066. return rc;
  1067. }
  1068. q6lsm_add_hdr(client, &cmd.hdr, sizeof(cmd), true);
  1069. cmd.hdr.opcode = LSM_SESSION_CMD_REGISTER_SOUND_MODEL;
  1070. cmd.model_addr_lsw = lower_32_bits(client->sound_model.phys);
  1071. cmd.model_addr_msw = msm_audio_populate_upper_32_bits(
  1072. client->sound_model.phys);
  1073. cmd.model_size = client->sound_model.size;
  1074. /* read updated mem_map_handle by q6lsm_mmapcallback */
  1075. rmb();
  1076. cmd.mem_map_handle = client->sound_model.mem_map_handle;
  1077. pr_debug("%s: addr %pK, size %d, handle 0x%x\n", __func__,
  1078. &client->sound_model.phys, cmd.model_size, cmd.mem_map_handle);
  1079. rc = q6lsm_apr_send_pkt(client, client->apr, &cmd, true, NULL);
  1080. if (rc)
  1081. pr_err("%s: Failed cmd op[0x%x]rc[%d]\n", __func__,
  1082. cmd.hdr.opcode, rc);
  1083. else
  1084. pr_debug("%s: Register sound model succeeded\n", __func__);
  1085. return rc;
  1086. }
  1087. EXPORT_SYMBOL(q6lsm_register_sound_model);
  1088. /**
  1089. * q6lsm_deregister_sound_model -
  1090. * De-register LSM snd model
  1091. *
  1092. * @client: LSM client handle
  1093. *
  1094. * Returns 0 on success or error on failure
  1095. */
  1096. int q6lsm_deregister_sound_model(struct lsm_client *client)
  1097. {
  1098. int rc;
  1099. struct lsm_cmd_reg_snd_model cmd;
  1100. if (!client) {
  1101. pr_err("APR handle NULL\n");
  1102. return -EINVAL;
  1103. }
  1104. if (!client->apr) {
  1105. pr_err("APR client handle NULL\n");
  1106. return -EINVAL;
  1107. }
  1108. if (CHECK_SESSION(client->session)) {
  1109. pr_err("%s: session[%d]", __func__, client->session);
  1110. return -EINVAL;
  1111. }
  1112. memset(&cmd, 0, sizeof(cmd));
  1113. q6lsm_add_hdr(client, &cmd.hdr, sizeof(cmd.hdr), false);
  1114. cmd.hdr.opcode = LSM_SESSION_CMD_DEREGISTER_SOUND_MODEL;
  1115. rc = q6lsm_apr_send_pkt(client, client->apr, &cmd.hdr, true, NULL);
  1116. if (rc) {
  1117. pr_err("%s: Failed cmd opcode 0x%x, rc %d\n", __func__,
  1118. cmd.hdr.opcode, rc);
  1119. } else {
  1120. pr_debug("%s: Deregister sound model succeeded\n", __func__);
  1121. }
  1122. q6lsm_snd_model_buf_free(client);
  1123. return rc;
  1124. }
  1125. EXPORT_SYMBOL(q6lsm_deregister_sound_model);
  1126. static void q6lsm_add_mmaphdr(struct lsm_client *client, struct apr_hdr *hdr,
  1127. u32 pkt_size, u32 cmd_flg, u32 token)
  1128. {
  1129. pr_debug("%s: pkt size=%d cmd_flg=%d session=%d\n", __func__, pkt_size,
  1130. cmd_flg, client->session);
  1131. hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1132. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1133. hdr->src_port = 0x00;
  1134. hdr->dest_port = client->session;
  1135. if (cmd_flg)
  1136. hdr->token = token;
  1137. hdr->pkt_size = pkt_size;
  1138. }
  1139. static int q6lsm_memory_map_regions(struct lsm_client *client,
  1140. dma_addr_t dma_addr_p, uint32_t dma_buf_sz,
  1141. uint32_t *mmap_p)
  1142. {
  1143. struct avs_cmd_shared_mem_map_regions *mmap_regions = NULL;
  1144. struct avs_shared_map_region_payload *mregions = NULL;
  1145. void *mmap_region_cmd = NULL;
  1146. void *payload = NULL;
  1147. int rc;
  1148. int cmd_size = 0;
  1149. pr_debug("%s: dma_addr_p 0x%pK, dma_buf_sz %d, mmap_p 0x%pK, session %d\n",
  1150. __func__, &dma_addr_p, dma_buf_sz, mmap_p,
  1151. client->session);
  1152. if (CHECK_SESSION(client->session)) {
  1153. pr_err("%s: session[%d]", __func__, client->session);
  1154. return -EINVAL;
  1155. }
  1156. cmd_size = sizeof(struct avs_cmd_shared_mem_map_regions) +
  1157. sizeof(struct avs_shared_map_region_payload);
  1158. mmap_region_cmd = kzalloc(cmd_size, GFP_KERNEL);
  1159. if (!mmap_region_cmd)
  1160. return -ENOMEM;
  1161. mmap_regions = (struct avs_cmd_shared_mem_map_regions *)mmap_region_cmd;
  1162. q6lsm_add_mmaphdr(client, &mmap_regions->hdr, cmd_size, true,
  1163. (client->session << 8));
  1164. mmap_regions->hdr.opcode = LSM_SESSION_CMD_SHARED_MEM_MAP_REGIONS;
  1165. mmap_regions->mem_pool_id = ADSP_MEMORY_MAP_SHMEM8_4K_POOL;
  1166. mmap_regions->num_regions = 1;
  1167. mmap_regions->property_flag = 0x00;
  1168. payload = ((u8 *)mmap_region_cmd +
  1169. sizeof(struct avs_cmd_shared_mem_map_regions));
  1170. mregions = (struct avs_shared_map_region_payload *)payload;
  1171. mregions->shm_addr_lsw = lower_32_bits(dma_addr_p);
  1172. mregions->shm_addr_msw = msm_audio_populate_upper_32_bits(dma_addr_p);
  1173. mregions->mem_size_bytes = dma_buf_sz;
  1174. rc = q6lsm_apr_send_pkt(client, client->mmap_apr, mmap_region_cmd,
  1175. true, mmap_p);
  1176. if (rc)
  1177. pr_err("%s: Failed mmap_regions opcode 0x%x, rc %d\n",
  1178. __func__, mmap_regions->hdr.opcode, rc);
  1179. pr_debug("%s: leave %d\n", __func__, rc);
  1180. kfree(mmap_region_cmd);
  1181. return rc;
  1182. }
  1183. static int q6lsm_memory_unmap_regions(struct lsm_client *client,
  1184. uint32_t handle)
  1185. {
  1186. struct avs_cmd_shared_mem_unmap_regions unmap;
  1187. int rc = 0;
  1188. int cmd_size = 0;
  1189. if (CHECK_SESSION(client->session)) {
  1190. pr_err("%s: session[%d]", __func__, client->session);
  1191. return -EINVAL;
  1192. }
  1193. cmd_size = sizeof(struct avs_cmd_shared_mem_unmap_regions);
  1194. q6lsm_add_mmaphdr(client, &unmap.hdr, cmd_size,
  1195. true, (client->session << 8));
  1196. unmap.hdr.opcode = LSM_SESSION_CMD_SHARED_MEM_UNMAP_REGIONS;
  1197. unmap.mem_map_handle = handle;
  1198. pr_debug("%s: unmap handle 0x%x\n", __func__, unmap.mem_map_handle);
  1199. rc = q6lsm_apr_send_pkt(client, client->mmap_apr, &unmap, true,
  1200. NULL);
  1201. if (rc)
  1202. pr_err("%s: Failed mmap_regions opcode 0x%x rc %d\n",
  1203. __func__, unmap.hdr.opcode, rc);
  1204. return rc;
  1205. }
  1206. static int q6lsm_send_cal(struct lsm_client *client,
  1207. u32 set_params_opcode)
  1208. {
  1209. int rc = 0;
  1210. struct mem_mapping_hdr mem_hdr;
  1211. struct cal_block_data *cal_block = NULL;
  1212. memset(&mem_hdr, 0, sizeof(mem_hdr));
  1213. pr_debug("%s: Session id %d\n", __func__, client->session);
  1214. if (CHECK_SESSION(client->session)) {
  1215. pr_err("%s: session[%d]", __func__, client->session);
  1216. return -EINVAL;
  1217. }
  1218. if (lsm_common.cal_data[LSM_CAL_IDX] == NULL)
  1219. goto done;
  1220. mutex_lock(&lsm_common.cal_data[LSM_CAL_IDX]->lock);
  1221. cal_block = cal_utils_get_only_cal_block(
  1222. lsm_common.cal_data[LSM_CAL_IDX]);
  1223. if (!cal_block || cal_block->cal_data.size <= 0) {
  1224. pr_debug("%s: No cal to send!\n", __func__);
  1225. goto unlock;
  1226. }
  1227. if (cal_block->cal_data.size != client->lsm_cal_size) {
  1228. pr_err("%s: Cal size %zd doesn't match lsm cal size %d\n",
  1229. __func__, cal_block->cal_data.size,
  1230. client->lsm_cal_size);
  1231. rc = -EINVAL;
  1232. goto unlock;
  1233. }
  1234. /* Cache mmap address, only map once or if new addr */
  1235. lsm_common.common_client[client->session].session = client->session;
  1236. mem_hdr.data_payload_addr_lsw = lower_32_bits(client->lsm_cal_phy_addr);
  1237. mem_hdr.data_payload_addr_msw =
  1238. msm_audio_populate_upper_32_bits(client->lsm_cal_phy_addr);
  1239. mem_hdr.mem_map_handle = client->sound_model.mem_map_handle;
  1240. pr_debug("%s: Cal Size = %zd", __func__, cal_block->cal_data.size);
  1241. rc = q6lsm_set_params(client, &mem_hdr, NULL, cal_block->cal_data.size,
  1242. set_params_opcode);
  1243. if (rc)
  1244. pr_err("%s: Failed set_params, rc %d\n", __func__, rc);
  1245. unlock:
  1246. mutex_unlock(&lsm_common.cal_data[LSM_CAL_IDX]->lock);
  1247. done:
  1248. return rc;
  1249. }
  1250. /**
  1251. * q6lsm_snd_model_buf_free -
  1252. * Free memory for LSM snd model
  1253. *
  1254. * @client: LSM client handle
  1255. *
  1256. * Returns 0 on success or error on failure
  1257. */
  1258. int q6lsm_snd_model_buf_free(struct lsm_client *client)
  1259. {
  1260. int rc;
  1261. pr_debug("%s: Session id %d\n", __func__, client->session);
  1262. if (CHECK_SESSION(client->session)) {
  1263. pr_err("%s: session[%d]", __func__, client->session);
  1264. return -EINVAL;
  1265. }
  1266. mutex_lock(&client->cmd_lock);
  1267. rc = q6lsm_memory_unmap_regions(client,
  1268. client->sound_model.mem_map_handle);
  1269. if (rc)
  1270. pr_err("%s: CMD Memory_unmap_regions failed %d\n",
  1271. __func__, rc);
  1272. if (client->sound_model.data) {
  1273. msm_audio_ion_free(client->sound_model.dma_buf);
  1274. client->sound_model.dma_buf = NULL;
  1275. client->sound_model.data = NULL;
  1276. client->sound_model.phys = 0;
  1277. client->lsm_cal_phy_addr = 0;
  1278. client->lsm_cal_size = 0;
  1279. }
  1280. mutex_unlock(&client->cmd_lock);
  1281. return rc;
  1282. }
  1283. EXPORT_SYMBOL(q6lsm_snd_model_buf_free);
  1284. static struct lsm_client *q6lsm_get_lsm_client(int session_id)
  1285. {
  1286. unsigned long flags;
  1287. struct lsm_client *client = NULL;
  1288. spin_lock_irqsave(&lsm_session_lock, flags);
  1289. if (session_id < LSM_MIN_SESSION_ID || session_id > LSM_MAX_SESSION_ID)
  1290. pr_err("%s: Invalid session %d\n", __func__, session_id);
  1291. else if (!lsm_session[session_id])
  1292. pr_err("%s: Not an active session %d\n", __func__, session_id);
  1293. else
  1294. client = lsm_session[session_id];
  1295. spin_unlock_irqrestore(&lsm_session_lock, flags);
  1296. return client;
  1297. }
  1298. /*
  1299. * q6lsm_mmapcallback : atomic context
  1300. */
  1301. static int q6lsm_mmapcallback(struct apr_client_data *data, void *priv)
  1302. {
  1303. unsigned long flags;
  1304. uint32_t command;
  1305. uint32_t retcode;
  1306. uint32_t sid;
  1307. const uint32_t *payload = data->payload;
  1308. struct lsm_client *client = NULL;
  1309. if (data->opcode == RESET_EVENTS) {
  1310. sid = (data->token >> 8) & 0x0F;
  1311. pr_debug("%s: SSR event received 0x%x, event 0x%x,\n"
  1312. "proc 0x%x SID 0x%x\n", __func__, data->opcode,
  1313. data->reset_event, data->reset_proc, sid);
  1314. apr_reset(lsm_common.apr);
  1315. lsm_common.apr = NULL;
  1316. atomic_set(&lsm_common.apr_users, 0);
  1317. lsm_common.common_client[sid].lsm_cal_phy_addr = 0;
  1318. cal_utils_clear_cal_block_q6maps(LSM_MAX_CAL_IDX,
  1319. lsm_common.cal_data);
  1320. lsm_common.set_custom_topology = 1;
  1321. return 0;
  1322. }
  1323. command = payload[0];
  1324. retcode = payload[1];
  1325. sid = (data->token >> 8) & 0x0F;
  1326. pr_debug("%s: opcode 0x%x command 0x%x return code 0x%x SID 0x%x\n",
  1327. __func__, data->opcode, command, retcode, sid);
  1328. client = q6lsm_get_lsm_client(sid);
  1329. if (!client) {
  1330. pr_debug("%s: Session %d already freed\n", __func__, sid);
  1331. return 0;
  1332. }
  1333. switch (data->opcode) {
  1334. case LSM_SESSION_CMDRSP_SHARED_MEM_MAP_REGIONS:
  1335. if (atomic_read(&client->cmd_state) == CMD_STATE_WAIT_RESP) {
  1336. spin_lock_irqsave(&mmap_lock, flags);
  1337. *mmap_handle_p = command;
  1338. /* spin_unlock_irqrestore implies barrier */
  1339. spin_unlock_irqrestore(&mmap_lock, flags);
  1340. atomic_set(&client->cmd_state, CMD_STATE_CLEARED);
  1341. wake_up(&client->cmd_wait);
  1342. }
  1343. break;
  1344. case APR_BASIC_RSP_RESULT:
  1345. switch (command) {
  1346. case LSM_SESSION_CMD_SHARED_MEM_UNMAP_REGIONS:
  1347. atomic_set(&client->cmd_state, CMD_STATE_CLEARED);
  1348. wake_up(&client->cmd_wait);
  1349. break;
  1350. case LSM_SESSION_CMD_SHARED_MEM_MAP_REGIONS:
  1351. if (retcode != 0) {
  1352. /* error state, signal to stop waiting */
  1353. if (atomic_read(&client->cmd_state) ==
  1354. CMD_STATE_WAIT_RESP) {
  1355. spin_lock_irqsave(&mmap_lock, flags);
  1356. /* implies barrier */
  1357. spin_unlock_irqrestore(&mmap_lock,
  1358. flags);
  1359. atomic_set(&client->cmd_state,
  1360. CMD_STATE_CLEARED);
  1361. wake_up(&client->cmd_wait);
  1362. }
  1363. }
  1364. break;
  1365. default:
  1366. pr_warn("%s: Unexpected command 0x%x\n", __func__,
  1367. command);
  1368. }
  1369. /* fallthrough */
  1370. default:
  1371. pr_debug("%s: command 0x%x return code 0x%x opcode 0x%x\n",
  1372. __func__, command, retcode, data->opcode);
  1373. break;
  1374. }
  1375. if (client->cb)
  1376. client->cb(data->opcode, data->token,
  1377. data->payload, client->priv);
  1378. return 0;
  1379. }
  1380. /**
  1381. * q6lsm_snd_model_buf_alloc -
  1382. * Allocate memory for LSM snd model
  1383. *
  1384. * @client: LSM client handle
  1385. * @len: size of sound model
  1386. * @allocate_module_data: flag to allocate for set_param payload
  1387. *
  1388. * Returns 0 on success or error on failure
  1389. */
  1390. int q6lsm_snd_model_buf_alloc(struct lsm_client *client, size_t len,
  1391. bool allocate_module_data)
  1392. {
  1393. int rc = -EINVAL;
  1394. struct cal_block_data *cal_block = NULL;
  1395. size_t pad_zero = 0, total_mem = 0;
  1396. if (!client || len <= LSM_ALIGN_BOUNDARY)
  1397. return rc;
  1398. mutex_lock(&client->cmd_lock);
  1399. mutex_lock(&lsm_common.cal_data[LSM_CAL_IDX]->lock);
  1400. cal_block = cal_utils_get_only_cal_block(
  1401. lsm_common.cal_data[LSM_CAL_IDX]);
  1402. if (cal_block == NULL)
  1403. goto fail;
  1404. pr_debug("%s:Snd Model len = %zd cal size %zd phys addr %pK", __func__,
  1405. len, cal_block->cal_data.size,
  1406. &cal_block->cal_data.paddr);
  1407. if (!cal_block->cal_data.paddr) {
  1408. pr_err("%s: No LSM calibration set for session", __func__);
  1409. rc = -EINVAL;
  1410. goto fail;
  1411. }
  1412. if (!client->sound_model.data) {
  1413. /*
  1414. * if sound module is sent as set_param
  1415. * Then memory needs to be allocated for
  1416. * set_param payload as well.
  1417. */
  1418. if (allocate_module_data)
  1419. len += sizeof(union param_hdrs);
  1420. client->sound_model.size = len;
  1421. pad_zero = (LSM_ALIGN_BOUNDARY -
  1422. (len % LSM_ALIGN_BOUNDARY));
  1423. if ((len > SIZE_MAX - pad_zero) ||
  1424. (len + pad_zero >
  1425. SIZE_MAX - cal_block->cal_data.size)) {
  1426. pr_err("%s: invalid allocation size, len = %zd, pad_zero =%zd, cal_size = %zd\n",
  1427. __func__, len, pad_zero,
  1428. cal_block->cal_data.size);
  1429. rc = -EINVAL;
  1430. goto fail;
  1431. }
  1432. total_mem = PAGE_ALIGN(pad_zero + len +
  1433. cal_block->cal_data.size);
  1434. pr_debug("%s: Pad zeros sound model %zd Total mem %zd\n",
  1435. __func__, pad_zero, total_mem);
  1436. rc = msm_audio_ion_alloc(&client->sound_model.dma_buf,
  1437. total_mem,
  1438. &client->sound_model.phys,
  1439. &len,
  1440. &client->sound_model.data);
  1441. if (rc) {
  1442. pr_err("%s: Audio ION alloc is failed, rc = %d\n",
  1443. __func__, rc);
  1444. goto fail;
  1445. }
  1446. pr_debug("%s: Length = %zd\n", __func__, len);
  1447. client->lsm_cal_phy_addr = (pad_zero +
  1448. client->sound_model.phys +
  1449. client->sound_model.size);
  1450. client->lsm_cal_size = cal_block->cal_data.size;
  1451. memcpy((client->sound_model.data + pad_zero +
  1452. client->sound_model.size),
  1453. (uint32_t *)cal_block->cal_data.kvaddr, client->lsm_cal_size);
  1454. pr_debug("%s: Copy cal start virt_addr %pK phy_addr %pK\n"
  1455. "Offset cal virtual Addr %pK\n", __func__,
  1456. client->sound_model.data, &client->sound_model.phys,
  1457. (pad_zero + client->sound_model.data +
  1458. client->sound_model.size));
  1459. } else {
  1460. pr_err("%s: sound model busy\n", __func__);
  1461. rc = -EBUSY;
  1462. goto fail;
  1463. }
  1464. mutex_unlock(&lsm_common.cal_data[LSM_CAL_IDX]->lock);
  1465. mutex_unlock(&client->cmd_lock);
  1466. rc = q6lsm_memory_map_regions(client, client->sound_model.phys,
  1467. len,
  1468. &client->sound_model.mem_map_handle);
  1469. if (rc) {
  1470. pr_err("%s: CMD Memory_map_regions failed %d\n", __func__, rc);
  1471. goto exit;
  1472. }
  1473. return 0;
  1474. fail:
  1475. mutex_unlock(&lsm_common.cal_data[LSM_CAL_IDX]->lock);
  1476. mutex_unlock(&client->cmd_lock);
  1477. exit:
  1478. q6lsm_snd_model_buf_free(client);
  1479. return rc;
  1480. }
  1481. EXPORT_SYMBOL(q6lsm_snd_model_buf_alloc);
  1482. static int q6lsm_cmd(struct lsm_client *client, int opcode, bool wait)
  1483. {
  1484. struct apr_hdr hdr;
  1485. int rc;
  1486. pr_debug("%s: enter opcode %x wait %d\n", __func__, opcode, wait);
  1487. q6lsm_add_hdr(client, &hdr, sizeof(hdr), true);
  1488. switch (opcode) {
  1489. case LSM_SESSION_CMD_START:
  1490. case LSM_SESSION_CMD_STOP:
  1491. case LSM_SESSION_CMD_CLOSE_TX:
  1492. case LSM_SESSION_CMD_EOB:
  1493. hdr.opcode = opcode;
  1494. break;
  1495. default:
  1496. pr_err("%s: Invalid opcode 0x%x\n", __func__, opcode);
  1497. return -EINVAL;
  1498. }
  1499. rc = q6lsm_apr_send_pkt(client, client->apr, &hdr, wait, NULL);
  1500. if (rc)
  1501. pr_err("%s: Failed commmand 0x%x\n", __func__, hdr.opcode);
  1502. pr_debug("%s: leave %d\n", __func__, rc);
  1503. return rc;
  1504. }
  1505. static int q6lsm_send_param_epd_thres(struct lsm_client *client, void *data,
  1506. struct param_hdr_v3 *param_info)
  1507. {
  1508. struct snd_lsm_ep_det_thres *ep_det_data = NULL;
  1509. struct lsm_param_epd_thres epd_thres;
  1510. int rc = 0;
  1511. memset(&epd_thres, 0, sizeof(epd_thres));
  1512. param_info->param_size = sizeof(epd_thres);
  1513. ep_det_data = (struct snd_lsm_ep_det_thres *) data;
  1514. epd_thres.minor_version = QLSM_PARAM_ID_MINOR_VERSION;
  1515. epd_thres.epd_begin = ep_det_data->epd_begin;
  1516. epd_thres.epd_end = ep_det_data->epd_end;
  1517. rc = q6lsm_pack_and_set_params(client, param_info,
  1518. (uint8_t *) &epd_thres,
  1519. LSM_SESSION_CMD_SET_PARAMS_V2);
  1520. if (unlikely(rc))
  1521. pr_err("%s: EPD_THRESHOLD failed, rc %d\n", __func__, rc);
  1522. return rc;
  1523. }
  1524. static int q6lsm_send_param_gain(struct lsm_client *client, u16 gain,
  1525. struct param_hdr_v3 *param_info)
  1526. {
  1527. struct lsm_param_gain lsm_gain;
  1528. int rc = 0;
  1529. memset(&lsm_gain, 0, sizeof(lsm_gain));
  1530. param_info->param_size = sizeof(lsm_gain);
  1531. lsm_gain.minor_version = QLSM_PARAM_ID_MINOR_VERSION;
  1532. lsm_gain.gain = gain;
  1533. rc = q6lsm_pack_and_set_params(client, param_info,
  1534. (uint8_t *) &lsm_gain,
  1535. LSM_SESSION_CMD_SET_PARAMS_V2);
  1536. if (unlikely(rc))
  1537. pr_err("%s: LSM_GAIN CMD send failed, rc %d\n", __func__, rc);
  1538. return rc;
  1539. }
  1540. /**
  1541. * q6lsm_set_one_param -
  1542. * command for LSM set params
  1543. *
  1544. * @client: LSM client handle
  1545. * p_info: Params info
  1546. * data: payload based on param type
  1547. * param_type: LSM param type
  1548. *
  1549. * Returns 0 on success or error on failure
  1550. */
  1551. int q6lsm_set_one_param(struct lsm_client *client,
  1552. struct lsm_params_info *p_info, void *data,
  1553. uint32_t param_type)
  1554. {
  1555. struct param_hdr_v3 param_info;
  1556. int rc = 0;
  1557. memset(&param_info, 0, sizeof(param_info));
  1558. switch (param_type) {
  1559. case LSM_ENDPOINT_DETECT_THRESHOLD: {
  1560. param_info.module_id = p_info->module_id;
  1561. param_info.instance_id = INSTANCE_ID_0;
  1562. param_info.param_id = p_info->param_id;
  1563. rc = q6lsm_send_param_epd_thres(client, data, &param_info);
  1564. if (rc)
  1565. pr_err("%s: LSM_ENDPOINT_DETECT_THRESHOLD failed, rc %d\n",
  1566. __func__, rc);
  1567. break;
  1568. }
  1569. case LSM_OPERATION_MODE: {
  1570. struct snd_lsm_detect_mode *det_mode = data;
  1571. if (det_mode->mode == LSM_MODE_KEYWORD_ONLY_DETECTION) {
  1572. client->mode = 0x01;
  1573. } else if (det_mode->mode == LSM_MODE_USER_KEYWORD_DETECTION) {
  1574. client->mode = 0x03;
  1575. } else {
  1576. pr_err("%s: Incorrect detection mode %d\n",
  1577. __func__, det_mode->mode);
  1578. return -EINVAL;
  1579. }
  1580. client->mode |= det_mode->detect_failure << 2;
  1581. param_info.module_id = p_info->module_id;
  1582. param_info.instance_id = INSTANCE_ID_0;
  1583. param_info.param_id = p_info->param_id;
  1584. rc = q6lsm_send_param_opmode(client, &param_info,
  1585. LSM_SESSION_CMD_SET_PARAMS_V2);
  1586. if (rc)
  1587. pr_err("%s: OPERATION_MODE failed, rc %d\n",
  1588. __func__, rc);
  1589. break;
  1590. }
  1591. case LSM_GAIN: {
  1592. struct snd_lsm_gain *lsm_gain = (struct snd_lsm_gain *) data;
  1593. param_info.module_id = p_info->module_id;
  1594. param_info.instance_id = INSTANCE_ID_0;
  1595. param_info.param_id = p_info->param_id;
  1596. rc = q6lsm_send_param_gain(client, lsm_gain->gain, &param_info);
  1597. if (rc)
  1598. pr_err("%s: LSM_GAIN command failed, rc %d\n",
  1599. __func__, rc);
  1600. break;
  1601. }
  1602. case LSM_MIN_CONFIDENCE_LEVELS:
  1603. param_info.module_id = p_info->module_id;
  1604. param_info.instance_id = INSTANCE_ID_0;
  1605. param_info.param_id = p_info->param_id;
  1606. rc = q6lsm_send_confidence_levels(
  1607. client, &param_info, LSM_SESSION_CMD_SET_PARAMS_V2);
  1608. if (rc)
  1609. pr_err("%s: CONFIDENCE_LEVELS cmd failed, rc %d\n",
  1610. __func__, rc);
  1611. break;
  1612. case LSM_POLLING_ENABLE: {
  1613. struct snd_lsm_poll_enable *lsm_poll_enable =
  1614. (struct snd_lsm_poll_enable *) data;
  1615. param_info.module_id = p_info->module_id;
  1616. param_info.instance_id = INSTANCE_ID_0;
  1617. param_info.param_id = p_info->param_id;
  1618. rc = q6lsm_send_param_polling_enable(
  1619. client, lsm_poll_enable->poll_en, &param_info,
  1620. LSM_SESSION_CMD_SET_PARAMS_V2);
  1621. if (rc)
  1622. pr_err("%s: POLLING ENABLE cmd failed, rc %d\n",
  1623. __func__, rc);
  1624. break;
  1625. }
  1626. case LSM_REG_SND_MODEL: {
  1627. struct mem_mapping_hdr mem_hdr;
  1628. u32 payload_size;
  1629. memset(&mem_hdr, 0, sizeof(mem_hdr));
  1630. if (q6common_is_instance_id_supported())
  1631. payload_size = p_info->param_size +
  1632. sizeof(struct param_hdr_v3);
  1633. else
  1634. payload_size = p_info->param_size +
  1635. sizeof(struct param_hdr_v2);
  1636. mem_hdr.data_payload_addr_lsw =
  1637. lower_32_bits(client->sound_model.phys);
  1638. mem_hdr.data_payload_addr_msw =
  1639. msm_audio_populate_upper_32_bits(
  1640. client->sound_model.phys),
  1641. mem_hdr.mem_map_handle = client->sound_model.mem_map_handle;
  1642. rc = q6lsm_set_params(client, &mem_hdr, NULL, payload_size,
  1643. LSM_SESSION_CMD_SET_PARAMS_V2);
  1644. if (rc) {
  1645. pr_err("%s: REG_SND_MODEL failed, rc %d\n",
  1646. __func__, rc);
  1647. return rc;
  1648. }
  1649. rc = q6lsm_send_cal(client, LSM_SESSION_CMD_SET_PARAMS);
  1650. if (rc)
  1651. pr_err("%s: Failed to send lsm cal, err = %d\n",
  1652. __func__, rc);
  1653. break;
  1654. }
  1655. case LSM_DEREG_SND_MODEL: {
  1656. param_info.module_id = p_info->module_id;
  1657. param_info.instance_id = INSTANCE_ID_0;
  1658. param_info.param_id = p_info->param_id;
  1659. param_info.param_size = 0;
  1660. rc = q6lsm_pack_and_set_params(client, &param_info, NULL,
  1661. LSM_SESSION_CMD_SET_PARAMS_V2);
  1662. if (rc)
  1663. pr_err("%s: DEREG_SND_MODEL failed, rc %d\n",
  1664. __func__, rc);
  1665. break;
  1666. }
  1667. case LSM_CUSTOM_PARAMS: {
  1668. u32 param_size = p_info->param_size;
  1669. /* Check minimum size, V2 structure is smaller than V3 */
  1670. if (param_size < sizeof(struct param_hdr_v2)) {
  1671. pr_err("%s: Invalid param_size %d\n", __func__,
  1672. param_size);
  1673. return -EINVAL;
  1674. }
  1675. rc = q6lsm_set_params(client, NULL, data, param_size,
  1676. LSM_SESSION_CMD_SET_PARAMS_V2);
  1677. if (rc)
  1678. pr_err("%s: CUSTOM_PARAMS failed, rc %d\n",
  1679. __func__, rc);
  1680. break;
  1681. }
  1682. default:
  1683. pr_err("%s: wrong param_type 0x%x\n",
  1684. __func__, p_info->param_type);
  1685. }
  1686. return rc;
  1687. }
  1688. EXPORT_SYMBOL(q6lsm_set_one_param);
  1689. /**
  1690. * q6lsm_start -
  1691. * command for LSM start
  1692. *
  1693. * @client: LSM client handle
  1694. *
  1695. * Returns 0 on success or error on failure
  1696. */
  1697. int q6lsm_start(struct lsm_client *client, bool wait)
  1698. {
  1699. return q6lsm_cmd(client, LSM_SESSION_CMD_START, wait);
  1700. }
  1701. EXPORT_SYMBOL(q6lsm_start);
  1702. /**
  1703. * q6lsm_stop -
  1704. * command for LSM stop
  1705. *
  1706. * @client: LSM client handle
  1707. *
  1708. * Returns 0 on success or error on failure
  1709. */
  1710. int q6lsm_stop(struct lsm_client *client, bool wait)
  1711. {
  1712. return q6lsm_cmd(client, LSM_SESSION_CMD_STOP, wait);
  1713. }
  1714. EXPORT_SYMBOL(q6lsm_stop);
  1715. /**
  1716. * q6lsm_close -
  1717. * command for LSM close
  1718. *
  1719. * @client: LSM client handle
  1720. *
  1721. * Returns 0 on success or error on failure
  1722. */
  1723. int q6lsm_close(struct lsm_client *client)
  1724. {
  1725. return q6lsm_cmd(client, LSM_SESSION_CMD_CLOSE_TX, true);
  1726. }
  1727. EXPORT_SYMBOL(q6lsm_close);
  1728. /**
  1729. * q6lsm_lab_control -
  1730. * command to set LSM LAB control params
  1731. *
  1732. * @client: LSM client handle
  1733. * @enable: bool flag to enable or disable LAB on DSP
  1734. *
  1735. * Returns 0 on success or error on failure
  1736. */
  1737. int q6lsm_lab_control(struct lsm_client *client, u32 enable)
  1738. {
  1739. struct lsm_param_lab_enable lab_enable;
  1740. struct param_hdr_v3 lab_enable_hdr;
  1741. struct lsm_param_lab_config lab_config;
  1742. struct param_hdr_v3 lab_config_hdr;
  1743. int rc = 0;
  1744. memset(&lab_enable, 0, sizeof(lab_enable));
  1745. memset(&lab_enable_hdr, 0, sizeof(lab_enable_hdr));
  1746. memset(&lab_config, 0, sizeof(lab_config));
  1747. memset(&lab_config_hdr, 0, sizeof(lab_config_hdr));
  1748. if (!client) {
  1749. pr_err("%s: invalid param client %pK\n", __func__, client);
  1750. return -EINVAL;
  1751. }
  1752. /* enable/disable lab on dsp */
  1753. lab_enable_hdr.module_id = LSM_MODULE_ID_LAB;
  1754. lab_enable_hdr.instance_id = INSTANCE_ID_0;
  1755. lab_enable_hdr.param_id = LSM_PARAM_ID_LAB_ENABLE;
  1756. lab_enable_hdr.param_size = sizeof(lab_enable);
  1757. lab_enable.enable = (enable) ? 1 : 0;
  1758. rc = q6lsm_pack_and_set_params(client, &lab_enable_hdr,
  1759. (uint8_t *) &lab_enable,
  1760. LSM_SESSION_CMD_SET_PARAMS);
  1761. if (rc) {
  1762. pr_err("%s: Lab enable failed rc %d\n", __func__, rc);
  1763. return rc;
  1764. }
  1765. if (!enable)
  1766. goto exit;
  1767. /* lab session is being enabled set the config values */
  1768. lab_config_hdr.module_id = LSM_MODULE_ID_LAB;
  1769. lab_config_hdr.instance_id = INSTANCE_ID_0;
  1770. lab_config_hdr.param_id = LSM_PARAM_ID_LAB_CONFIG;
  1771. lab_config_hdr.param_size = sizeof(lab_config);
  1772. lab_config.minor_version = 1;
  1773. lab_config.wake_up_latency_ms = 250;
  1774. rc = q6lsm_pack_and_set_params(client, &lab_config_hdr,
  1775. (uint8_t *) &lab_config,
  1776. LSM_SESSION_CMD_SET_PARAMS);
  1777. if (rc) {
  1778. pr_err("%s: Lab config failed rc %d disable lab\n",
  1779. __func__, rc);
  1780. /* Lab config failed disable lab */
  1781. lab_enable.enable = 0;
  1782. if (q6lsm_pack_and_set_params(client, &lab_enable_hdr,
  1783. (uint8_t *) &lab_enable,
  1784. LSM_SESSION_CMD_SET_PARAMS))
  1785. pr_err("%s: Lab disable failed\n", __func__);
  1786. }
  1787. exit:
  1788. return rc;
  1789. }
  1790. EXPORT_SYMBOL(q6lsm_lab_control);
  1791. /**
  1792. * q6lsm_stop_lab -
  1793. * command to stop LSM LAB
  1794. *
  1795. * @client: LSM client handle
  1796. *
  1797. * Returns 0 on success or error on failure
  1798. */
  1799. int q6lsm_stop_lab(struct lsm_client *client)
  1800. {
  1801. int rc = 0;
  1802. if (!client) {
  1803. pr_err("%s: invalid param client %pK\n", __func__, client);
  1804. return -EINVAL;
  1805. }
  1806. rc = q6lsm_cmd(client, LSM_SESSION_CMD_EOB, true);
  1807. if (rc)
  1808. pr_err("%s: Lab stop failed %d\n", __func__, rc);
  1809. return rc;
  1810. }
  1811. EXPORT_SYMBOL(q6lsm_stop_lab);
  1812. /**
  1813. * q6lsm_read -
  1814. * command for LSM read
  1815. *
  1816. * @client: LSM client handle
  1817. * @lsm_cmd_read: LSM read command
  1818. *
  1819. * Returns 0 on success or error on failure
  1820. */
  1821. int q6lsm_read(struct lsm_client *client, struct lsm_cmd_read *read)
  1822. {
  1823. int rc = 0;
  1824. if (!client || !read) {
  1825. pr_err("%s: Invalid params client %pK read %pK\n", __func__,
  1826. client, read);
  1827. return -EINVAL;
  1828. }
  1829. pr_debug("%s: read call memmap handle %x address %x%x size %d\n",
  1830. __func__, read->mem_map_handle, read->buf_addr_msw,
  1831. read->buf_addr_lsw, read->buf_size);
  1832. q6lsm_add_hdr(client, &read->hdr, sizeof(struct lsm_cmd_read), true);
  1833. read->hdr.opcode = LSM_SESSION_CMD_READ;
  1834. rc = q6lsm_apr_send_pkt(client, client->apr, read, false, NULL);
  1835. if (rc)
  1836. pr_err("%s: read buffer call failed rc %d\n", __func__, rc);
  1837. return rc;
  1838. }
  1839. EXPORT_SYMBOL(q6lsm_read);
  1840. /**
  1841. * q6lsm_lab_buffer_alloc -
  1842. * Lab buffer allocation or de-alloc
  1843. *
  1844. * @client: LSM client handle
  1845. * @alloc: Allocate or free ion memory
  1846. *
  1847. * Returns 0 on success or error on failure
  1848. */
  1849. int q6lsm_lab_buffer_alloc(struct lsm_client *client, bool alloc)
  1850. {
  1851. int ret = 0, i = 0;
  1852. size_t allocate_size = 0, len = 0;
  1853. if (!client) {
  1854. pr_err("%s: invalid client\n", __func__);
  1855. return -EINVAL;
  1856. }
  1857. if (alloc) {
  1858. if (client->lab_buffer) {
  1859. pr_err("%s: buffers are allocated period count %d period size %d\n",
  1860. __func__,
  1861. client->hw_params.period_count,
  1862. client->hw_params.buf_sz);
  1863. return -EINVAL;
  1864. }
  1865. allocate_size = client->hw_params.period_count *
  1866. client->hw_params.buf_sz;
  1867. allocate_size = PAGE_ALIGN(allocate_size);
  1868. client->lab_buffer =
  1869. kzalloc(sizeof(struct lsm_lab_buffer) *
  1870. client->hw_params.period_count, GFP_KERNEL);
  1871. if (!client->lab_buffer) {
  1872. pr_err("%s: memory allocation for lab buffer failed count %d\n"
  1873. , __func__,
  1874. client->hw_params.period_count);
  1875. return -ENOMEM;
  1876. }
  1877. ret = msm_audio_ion_alloc(&client->lab_buffer[0].dma_buf,
  1878. allocate_size, &client->lab_buffer[0].phys,
  1879. &len,
  1880. &client->lab_buffer[0].data);
  1881. if (ret)
  1882. pr_err("%s: ion alloc failed ret %d size %zd\n",
  1883. __func__, ret, allocate_size);
  1884. else {
  1885. ret = q6lsm_memory_map_regions(client,
  1886. client->lab_buffer[0].phys, len,
  1887. &client->lab_buffer[0].mem_map_handle);
  1888. if (ret) {
  1889. pr_err("%s: memory map filed ret %d size %zd\n",
  1890. __func__, ret, len);
  1891. msm_audio_ion_free(
  1892. client->lab_buffer[0].dma_buf);
  1893. }
  1894. }
  1895. if (ret) {
  1896. pr_err("%s: alloc lab buffer failed ret %d\n",
  1897. __func__, ret);
  1898. kfree(client->lab_buffer);
  1899. client->lab_buffer = NULL;
  1900. } else {
  1901. pr_debug("%s: Memory map handle %x phys %pK size %d\n",
  1902. __func__,
  1903. client->lab_buffer[0].mem_map_handle,
  1904. &client->lab_buffer[0].phys,
  1905. client->hw_params.buf_sz);
  1906. for (i = 0; i < client->hw_params.period_count; i++) {
  1907. client->lab_buffer[i].phys =
  1908. client->lab_buffer[0].phys +
  1909. (i * client->hw_params.buf_sz);
  1910. client->lab_buffer[i].size =
  1911. client->hw_params.buf_sz;
  1912. client->lab_buffer[i].data =
  1913. (u8 *)(client->lab_buffer[0].data) +
  1914. (i * client->hw_params.buf_sz);
  1915. client->lab_buffer[i].mem_map_handle =
  1916. client->lab_buffer[0].mem_map_handle;
  1917. }
  1918. }
  1919. } else {
  1920. ret = q6lsm_memory_unmap_regions(client,
  1921. client->lab_buffer[0].mem_map_handle);
  1922. if (!ret)
  1923. msm_audio_ion_free(client->lab_buffer[0].dma_buf);
  1924. else
  1925. pr_err("%s: unmap failed not freeing memory\n",
  1926. __func__);
  1927. kfree(client->lab_buffer);
  1928. client->lab_buffer = NULL;
  1929. }
  1930. return ret;
  1931. }
  1932. EXPORT_SYMBOL(q6lsm_lab_buffer_alloc);
  1933. static int get_cal_type_index(int32_t cal_type)
  1934. {
  1935. int ret = -EINVAL;
  1936. switch (cal_type) {
  1937. case LSM_CUST_TOPOLOGY_CAL_TYPE:
  1938. ret = LSM_CUSTOM_TOP_IDX;
  1939. break;
  1940. case LSM_TOPOLOGY_CAL_TYPE:
  1941. ret = LSM_TOP_IDX;
  1942. break;
  1943. case LSM_CAL_TYPE:
  1944. ret = LSM_CAL_IDX;
  1945. break;
  1946. default:
  1947. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  1948. }
  1949. return ret;
  1950. }
  1951. static int q6lsm_alloc_cal(int32_t cal_type,
  1952. size_t data_size, void *data)
  1953. {
  1954. int ret = 0;
  1955. int cal_index;
  1956. pr_debug("%s:\n", __func__);
  1957. cal_index = get_cal_type_index(cal_type);
  1958. if (cal_index < 0) {
  1959. pr_err("%s: could not get cal index %d!\n",
  1960. __func__, cal_index);
  1961. ret = -EINVAL;
  1962. goto done;
  1963. }
  1964. ret = cal_utils_alloc_cal(data_size, data,
  1965. lsm_common.cal_data[cal_index], 0, NULL);
  1966. if (ret < 0) {
  1967. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  1968. __func__, ret, cal_type);
  1969. ret = -EINVAL;
  1970. goto done;
  1971. }
  1972. done:
  1973. return ret;
  1974. }
  1975. static int q6lsm_dealloc_cal(int32_t cal_type,
  1976. size_t data_size, void *data)
  1977. {
  1978. int ret = 0;
  1979. int cal_index;
  1980. pr_debug("%s:\n", __func__);
  1981. cal_index = get_cal_type_index(cal_type);
  1982. if (cal_index < 0) {
  1983. pr_err("%s: could not get cal index %d!\n",
  1984. __func__, cal_index);
  1985. ret = -EINVAL;
  1986. goto done;
  1987. }
  1988. ret = cal_utils_dealloc_cal(data_size, data,
  1989. lsm_common.cal_data[cal_index]);
  1990. if (ret < 0) {
  1991. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  1992. __func__, ret, cal_type);
  1993. ret = -EINVAL;
  1994. goto done;
  1995. }
  1996. done:
  1997. return ret;
  1998. }
  1999. static int q6lsm_set_cal(int32_t cal_type,
  2000. size_t data_size, void *data)
  2001. {
  2002. int ret = 0;
  2003. int cal_index;
  2004. pr_debug("%s:\n", __func__);
  2005. cal_index = get_cal_type_index(cal_type);
  2006. if (cal_index < 0) {
  2007. pr_err("%s: could not get cal index %d!\n",
  2008. __func__, cal_index);
  2009. ret = -EINVAL;
  2010. goto done;
  2011. }
  2012. ret = cal_utils_set_cal(data_size, data,
  2013. lsm_common.cal_data[cal_index], 0, NULL);
  2014. if (ret < 0) {
  2015. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  2016. __func__, ret, cal_type);
  2017. ret = -EINVAL;
  2018. goto done;
  2019. }
  2020. if (cal_index == LSM_CUSTOM_TOP_IDX) {
  2021. mutex_lock(&lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]->lock);
  2022. lsm_common.set_custom_topology = 1;
  2023. mutex_unlock(&lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]->lock);
  2024. }
  2025. done:
  2026. return ret;
  2027. }
  2028. static void lsm_delete_cal_data(void)
  2029. {
  2030. pr_debug("%s:\n", __func__);
  2031. cal_utils_destroy_cal_types(LSM_MAX_CAL_IDX, lsm_common.cal_data);
  2032. }
  2033. static int q6lsm_init_cal_data(void)
  2034. {
  2035. int ret = 0;
  2036. struct cal_type_info cal_type_info[] = {
  2037. {{LSM_CUST_TOPOLOGY_CAL_TYPE,
  2038. {q6lsm_alloc_cal, q6lsm_dealloc_cal, NULL,
  2039. q6lsm_set_cal, NULL, NULL} },
  2040. {NULL, NULL, cal_utils_match_buf_num} },
  2041. {{LSM_TOPOLOGY_CAL_TYPE,
  2042. {NULL, NULL, NULL,
  2043. q6lsm_set_cal, NULL, NULL} },
  2044. {NULL, NULL, cal_utils_match_buf_num} },
  2045. {{LSM_CAL_TYPE,
  2046. {q6lsm_alloc_cal, q6lsm_dealloc_cal, NULL,
  2047. q6lsm_set_cal, NULL, NULL} },
  2048. {NULL, NULL, cal_utils_match_buf_num} }
  2049. };
  2050. pr_debug("%s:\n", __func__);
  2051. ret = cal_utils_create_cal_types(LSM_MAX_CAL_IDX,
  2052. lsm_common.cal_data, cal_type_info);
  2053. if (ret < 0) {
  2054. pr_err("%s: could not create cal type!\n",
  2055. __func__);
  2056. ret = -EINVAL;
  2057. goto err;
  2058. }
  2059. return ret;
  2060. err:
  2061. lsm_delete_cal_data();
  2062. return ret;
  2063. }
  2064. int __init q6lsm_init(void)
  2065. {
  2066. int i = 0;
  2067. pr_debug("%s:\n", __func__);
  2068. memset(&lsm_common, 0, sizeof(lsm_common));
  2069. spin_lock_init(&lsm_session_lock);
  2070. spin_lock_init(&mmap_lock);
  2071. mutex_init(&lsm_common.apr_lock);
  2072. for (; i <= LSM_MAX_SESSION_ID; i++) {
  2073. lsm_common.common_client[i].session = LSM_CONTROL_SESSION;
  2074. init_waitqueue_head(&lsm_common.common_client[i].cmd_wait);
  2075. mutex_init(&lsm_common.common_client[i].cmd_lock);
  2076. atomic_set(&lsm_common.common_client[i].cmd_state,
  2077. CMD_STATE_CLEARED);
  2078. }
  2079. if (q6lsm_init_cal_data())
  2080. pr_err("%s: could not init cal data!\n", __func__);
  2081. return 0;
  2082. }
  2083. void q6lsm_exit(void)
  2084. {
  2085. lsm_delete_cal_data();
  2086. }