q6lsm.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383
  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] = NULL;
  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. case LSM_DET_EVENT_TYPE: {
  1683. struct lsm_param_det_event_type det_event_type;
  1684. struct snd_lsm_det_event_type *det_event_data =
  1685. (struct snd_lsm_det_event_type *)data;
  1686. param_info.module_id = p_info->module_id;
  1687. param_info.instance_id = INSTANCE_ID_0;
  1688. param_info.param_id = p_info->param_id;
  1689. param_info.param_size = sizeof(det_event_type);
  1690. memset(&det_event_type, 0, sizeof(det_event_type));
  1691. det_event_type.minor_version = QLSM_PARAM_ID_MINOR_VERSION;
  1692. det_event_type.event_type = det_event_data->event_type;
  1693. det_event_type.mode = det_event_data->mode;
  1694. rc = q6lsm_pack_and_set_params(client, &param_info,
  1695. (uint8_t *)&det_event_type,
  1696. LSM_SESSION_CMD_SET_PARAMS_V2);
  1697. if (rc)
  1698. pr_err("%s: DET_EVENT_TYPE cmd failed, rc %d\n",
  1699. __func__, rc);
  1700. break;
  1701. }
  1702. default:
  1703. pr_err("%s: wrong param_type 0x%x\n",
  1704. __func__, p_info->param_type);
  1705. }
  1706. return rc;
  1707. }
  1708. EXPORT_SYMBOL(q6lsm_set_one_param);
  1709. /**
  1710. * q6lsm_start -
  1711. * command for LSM start
  1712. *
  1713. * @client: LSM client handle
  1714. *
  1715. * Returns 0 on success or error on failure
  1716. */
  1717. int q6lsm_start(struct lsm_client *client, bool wait)
  1718. {
  1719. return q6lsm_cmd(client, LSM_SESSION_CMD_START, wait);
  1720. }
  1721. EXPORT_SYMBOL(q6lsm_start);
  1722. /**
  1723. * q6lsm_stop -
  1724. * command for LSM stop
  1725. *
  1726. * @client: LSM client handle
  1727. *
  1728. * Returns 0 on success or error on failure
  1729. */
  1730. int q6lsm_stop(struct lsm_client *client, bool wait)
  1731. {
  1732. return q6lsm_cmd(client, LSM_SESSION_CMD_STOP, wait);
  1733. }
  1734. EXPORT_SYMBOL(q6lsm_stop);
  1735. /**
  1736. * q6lsm_close -
  1737. * command for LSM close
  1738. *
  1739. * @client: LSM client handle
  1740. *
  1741. * Returns 0 on success or error on failure
  1742. */
  1743. int q6lsm_close(struct lsm_client *client)
  1744. {
  1745. return q6lsm_cmd(client, LSM_SESSION_CMD_CLOSE_TX, true);
  1746. }
  1747. EXPORT_SYMBOL(q6lsm_close);
  1748. /**
  1749. * q6lsm_lab_control -
  1750. * command to set LSM LAB control params
  1751. *
  1752. * @client: LSM client handle
  1753. * @enable: bool flag to enable or disable LAB on DSP
  1754. *
  1755. * Returns 0 on success or error on failure
  1756. */
  1757. int q6lsm_lab_control(struct lsm_client *client, u32 enable)
  1758. {
  1759. struct lsm_param_lab_enable lab_enable;
  1760. struct param_hdr_v3 lab_enable_hdr;
  1761. struct lsm_param_lab_config lab_config;
  1762. struct param_hdr_v3 lab_config_hdr;
  1763. int rc = 0;
  1764. memset(&lab_enable, 0, sizeof(lab_enable));
  1765. memset(&lab_enable_hdr, 0, sizeof(lab_enable_hdr));
  1766. memset(&lab_config, 0, sizeof(lab_config));
  1767. memset(&lab_config_hdr, 0, sizeof(lab_config_hdr));
  1768. if (!client) {
  1769. pr_err("%s: invalid param client %pK\n", __func__, client);
  1770. return -EINVAL;
  1771. }
  1772. /* enable/disable lab on dsp */
  1773. lab_enable_hdr.module_id = LSM_MODULE_ID_LAB;
  1774. lab_enable_hdr.instance_id = INSTANCE_ID_0;
  1775. lab_enable_hdr.param_id = LSM_PARAM_ID_LAB_ENABLE;
  1776. lab_enable_hdr.param_size = sizeof(lab_enable);
  1777. lab_enable.enable = (enable) ? 1 : 0;
  1778. rc = q6lsm_pack_and_set_params(client, &lab_enable_hdr,
  1779. (uint8_t *) &lab_enable,
  1780. LSM_SESSION_CMD_SET_PARAMS);
  1781. if (rc) {
  1782. pr_err("%s: Lab enable failed rc %d\n", __func__, rc);
  1783. return rc;
  1784. }
  1785. if (!enable)
  1786. goto exit;
  1787. /* lab session is being enabled set the config values */
  1788. lab_config_hdr.module_id = LSM_MODULE_ID_LAB;
  1789. lab_config_hdr.instance_id = INSTANCE_ID_0;
  1790. lab_config_hdr.param_id = LSM_PARAM_ID_LAB_CONFIG;
  1791. lab_config_hdr.param_size = sizeof(lab_config);
  1792. lab_config.minor_version = 1;
  1793. lab_config.wake_up_latency_ms = 250;
  1794. rc = q6lsm_pack_and_set_params(client, &lab_config_hdr,
  1795. (uint8_t *) &lab_config,
  1796. LSM_SESSION_CMD_SET_PARAMS);
  1797. if (rc) {
  1798. pr_err("%s: Lab config failed rc %d disable lab\n",
  1799. __func__, rc);
  1800. /* Lab config failed disable lab */
  1801. lab_enable.enable = 0;
  1802. if (q6lsm_pack_and_set_params(client, &lab_enable_hdr,
  1803. (uint8_t *) &lab_enable,
  1804. LSM_SESSION_CMD_SET_PARAMS))
  1805. pr_err("%s: Lab disable failed\n", __func__);
  1806. }
  1807. exit:
  1808. return rc;
  1809. }
  1810. EXPORT_SYMBOL(q6lsm_lab_control);
  1811. /**
  1812. * q6lsm_stop_lab -
  1813. * command to stop LSM LAB
  1814. *
  1815. * @client: LSM client handle
  1816. *
  1817. * Returns 0 on success or error on failure
  1818. */
  1819. int q6lsm_stop_lab(struct lsm_client *client)
  1820. {
  1821. int rc = 0;
  1822. if (!client) {
  1823. pr_err("%s: invalid param client %pK\n", __func__, client);
  1824. return -EINVAL;
  1825. }
  1826. rc = q6lsm_cmd(client, LSM_SESSION_CMD_EOB, true);
  1827. if (rc)
  1828. pr_err("%s: Lab stop failed %d\n", __func__, rc);
  1829. return rc;
  1830. }
  1831. EXPORT_SYMBOL(q6lsm_stop_lab);
  1832. /**
  1833. * q6lsm_read -
  1834. * command for LSM read
  1835. *
  1836. * @client: LSM client handle
  1837. * @lsm_cmd_read: LSM read command
  1838. *
  1839. * Returns 0 on success or error on failure
  1840. */
  1841. int q6lsm_read(struct lsm_client *client, struct lsm_cmd_read *read)
  1842. {
  1843. int rc = 0;
  1844. if (!client || !read) {
  1845. pr_err("%s: Invalid params client %pK read %pK\n", __func__,
  1846. client, read);
  1847. return -EINVAL;
  1848. }
  1849. pr_debug("%s: read call memmap handle %x address %x%x size %d\n",
  1850. __func__, read->mem_map_handle, read->buf_addr_msw,
  1851. read->buf_addr_lsw, read->buf_size);
  1852. q6lsm_add_hdr(client, &read->hdr, sizeof(struct lsm_cmd_read), true);
  1853. read->hdr.opcode = LSM_SESSION_CMD_READ;
  1854. rc = q6lsm_apr_send_pkt(client, client->apr, read, false, NULL);
  1855. if (rc)
  1856. pr_err("%s: read buffer call failed rc %d\n", __func__, rc);
  1857. return rc;
  1858. }
  1859. EXPORT_SYMBOL(q6lsm_read);
  1860. /**
  1861. * q6lsm_lab_buffer_alloc -
  1862. * Lab buffer allocation or de-alloc
  1863. *
  1864. * @client: LSM client handle
  1865. * @alloc: Allocate or free ion memory
  1866. *
  1867. * Returns 0 on success or error on failure
  1868. */
  1869. int q6lsm_lab_buffer_alloc(struct lsm_client *client, bool alloc)
  1870. {
  1871. int ret = 0, i = 0;
  1872. size_t allocate_size = 0, len = 0;
  1873. if (!client) {
  1874. pr_err("%s: invalid client\n", __func__);
  1875. return -EINVAL;
  1876. }
  1877. if (alloc) {
  1878. if (client->lab_buffer) {
  1879. pr_err("%s: buffers are allocated period count %d period size %d\n",
  1880. __func__,
  1881. client->hw_params.period_count,
  1882. client->hw_params.buf_sz);
  1883. return -EINVAL;
  1884. }
  1885. allocate_size = client->hw_params.period_count *
  1886. client->hw_params.buf_sz;
  1887. allocate_size = PAGE_ALIGN(allocate_size);
  1888. client->lab_buffer =
  1889. kzalloc(sizeof(struct lsm_lab_buffer) *
  1890. client->hw_params.period_count, GFP_KERNEL);
  1891. if (!client->lab_buffer) {
  1892. pr_err("%s: memory allocation for lab buffer failed count %d\n"
  1893. , __func__,
  1894. client->hw_params.period_count);
  1895. return -ENOMEM;
  1896. }
  1897. ret = msm_audio_ion_alloc(&client->lab_buffer[0].dma_buf,
  1898. allocate_size, &client->lab_buffer[0].phys,
  1899. &len,
  1900. &client->lab_buffer[0].data);
  1901. if (ret)
  1902. pr_err("%s: ion alloc failed ret %d size %zd\n",
  1903. __func__, ret, allocate_size);
  1904. else {
  1905. ret = q6lsm_memory_map_regions(client,
  1906. client->lab_buffer[0].phys, len,
  1907. &client->lab_buffer[0].mem_map_handle);
  1908. if (ret) {
  1909. pr_err("%s: memory map filed ret %d size %zd\n",
  1910. __func__, ret, len);
  1911. msm_audio_ion_free(
  1912. client->lab_buffer[0].dma_buf);
  1913. }
  1914. }
  1915. if (ret) {
  1916. pr_err("%s: alloc lab buffer failed ret %d\n",
  1917. __func__, ret);
  1918. kfree(client->lab_buffer);
  1919. client->lab_buffer = NULL;
  1920. } else {
  1921. pr_debug("%s: Memory map handle %x phys %pK size %d\n",
  1922. __func__,
  1923. client->lab_buffer[0].mem_map_handle,
  1924. &client->lab_buffer[0].phys,
  1925. client->hw_params.buf_sz);
  1926. for (i = 0; i < client->hw_params.period_count; i++) {
  1927. client->lab_buffer[i].phys =
  1928. client->lab_buffer[0].phys +
  1929. (i * client->hw_params.buf_sz);
  1930. client->lab_buffer[i].size =
  1931. client->hw_params.buf_sz;
  1932. client->lab_buffer[i].data =
  1933. (u8 *)(client->lab_buffer[0].data) +
  1934. (i * client->hw_params.buf_sz);
  1935. client->lab_buffer[i].mem_map_handle =
  1936. client->lab_buffer[0].mem_map_handle;
  1937. }
  1938. }
  1939. } else {
  1940. ret = q6lsm_memory_unmap_regions(client,
  1941. client->lab_buffer[0].mem_map_handle);
  1942. if (!ret)
  1943. msm_audio_ion_free(client->lab_buffer[0].dma_buf);
  1944. else
  1945. pr_err("%s: unmap failed not freeing memory\n",
  1946. __func__);
  1947. kfree(client->lab_buffer);
  1948. client->lab_buffer = NULL;
  1949. }
  1950. return ret;
  1951. }
  1952. EXPORT_SYMBOL(q6lsm_lab_buffer_alloc);
  1953. static int get_cal_type_index(int32_t cal_type)
  1954. {
  1955. int ret = -EINVAL;
  1956. switch (cal_type) {
  1957. case LSM_CUST_TOPOLOGY_CAL_TYPE:
  1958. ret = LSM_CUSTOM_TOP_IDX;
  1959. break;
  1960. case LSM_TOPOLOGY_CAL_TYPE:
  1961. ret = LSM_TOP_IDX;
  1962. break;
  1963. case LSM_CAL_TYPE:
  1964. ret = LSM_CAL_IDX;
  1965. break;
  1966. default:
  1967. pr_err("%s: invalid cal type %d!\n", __func__, cal_type);
  1968. }
  1969. return ret;
  1970. }
  1971. static int q6lsm_alloc_cal(int32_t cal_type,
  1972. size_t data_size, void *data)
  1973. {
  1974. int ret = 0;
  1975. int cal_index;
  1976. pr_debug("%s:\n", __func__);
  1977. cal_index = get_cal_type_index(cal_type);
  1978. if (cal_index < 0) {
  1979. pr_err("%s: could not get cal index %d!\n",
  1980. __func__, cal_index);
  1981. ret = -EINVAL;
  1982. goto done;
  1983. }
  1984. ret = cal_utils_alloc_cal(data_size, data,
  1985. lsm_common.cal_data[cal_index], 0, NULL);
  1986. if (ret < 0) {
  1987. pr_err("%s: cal_utils_alloc_block failed, ret = %d, cal type = %d!\n",
  1988. __func__, ret, cal_type);
  1989. ret = -EINVAL;
  1990. goto done;
  1991. }
  1992. done:
  1993. return ret;
  1994. }
  1995. static int q6lsm_dealloc_cal(int32_t cal_type,
  1996. size_t data_size, void *data)
  1997. {
  1998. int ret = 0;
  1999. int cal_index;
  2000. pr_debug("%s:\n", __func__);
  2001. cal_index = get_cal_type_index(cal_type);
  2002. if (cal_index < 0) {
  2003. pr_err("%s: could not get cal index %d!\n",
  2004. __func__, cal_index);
  2005. ret = -EINVAL;
  2006. goto done;
  2007. }
  2008. ret = cal_utils_dealloc_cal(data_size, data,
  2009. lsm_common.cal_data[cal_index]);
  2010. if (ret < 0) {
  2011. pr_err("%s: cal_utils_dealloc_block failed, ret = %d, cal type = %d!\n",
  2012. __func__, ret, cal_type);
  2013. ret = -EINVAL;
  2014. goto done;
  2015. }
  2016. done:
  2017. return ret;
  2018. }
  2019. static int q6lsm_set_cal(int32_t cal_type,
  2020. size_t data_size, void *data)
  2021. {
  2022. int ret = 0;
  2023. int cal_index;
  2024. pr_debug("%s:\n", __func__);
  2025. cal_index = get_cal_type_index(cal_type);
  2026. if (cal_index < 0) {
  2027. pr_err("%s: could not get cal index %d!\n",
  2028. __func__, cal_index);
  2029. ret = -EINVAL;
  2030. goto done;
  2031. }
  2032. ret = cal_utils_set_cal(data_size, data,
  2033. lsm_common.cal_data[cal_index], 0, NULL);
  2034. if (ret < 0) {
  2035. pr_err("%s: cal_utils_set_cal failed, ret = %d, cal type = %d!\n",
  2036. __func__, ret, cal_type);
  2037. ret = -EINVAL;
  2038. goto done;
  2039. }
  2040. if (cal_index == LSM_CUSTOM_TOP_IDX) {
  2041. mutex_lock(&lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]->lock);
  2042. lsm_common.set_custom_topology = 1;
  2043. mutex_unlock(&lsm_common.cal_data[LSM_CUSTOM_TOP_IDX]->lock);
  2044. }
  2045. done:
  2046. return ret;
  2047. }
  2048. static void lsm_delete_cal_data(void)
  2049. {
  2050. pr_debug("%s:\n", __func__);
  2051. cal_utils_destroy_cal_types(LSM_MAX_CAL_IDX, lsm_common.cal_data);
  2052. }
  2053. static int q6lsm_init_cal_data(void)
  2054. {
  2055. int ret = 0;
  2056. struct cal_type_info cal_type_info[] = {
  2057. {{LSM_CUST_TOPOLOGY_CAL_TYPE,
  2058. {q6lsm_alloc_cal, q6lsm_dealloc_cal, NULL,
  2059. q6lsm_set_cal, NULL, NULL} },
  2060. {NULL, NULL, cal_utils_match_buf_num} },
  2061. {{LSM_TOPOLOGY_CAL_TYPE,
  2062. {NULL, NULL, NULL,
  2063. q6lsm_set_cal, NULL, NULL} },
  2064. {NULL, NULL, cal_utils_match_buf_num} },
  2065. {{LSM_CAL_TYPE,
  2066. {q6lsm_alloc_cal, q6lsm_dealloc_cal, NULL,
  2067. q6lsm_set_cal, NULL, NULL} },
  2068. {NULL, NULL, cal_utils_match_buf_num} }
  2069. };
  2070. pr_debug("%s:\n", __func__);
  2071. ret = cal_utils_create_cal_types(LSM_MAX_CAL_IDX,
  2072. lsm_common.cal_data, cal_type_info);
  2073. if (ret < 0) {
  2074. pr_err("%s: could not create cal type!\n",
  2075. __func__);
  2076. ret = -EINVAL;
  2077. goto err;
  2078. }
  2079. return ret;
  2080. err:
  2081. lsm_delete_cal_data();
  2082. return ret;
  2083. }
  2084. int __init q6lsm_init(void)
  2085. {
  2086. int i = 0;
  2087. pr_debug("%s:\n", __func__);
  2088. memset(&lsm_common, 0, sizeof(lsm_common));
  2089. spin_lock_init(&lsm_session_lock);
  2090. spin_lock_init(&mmap_lock);
  2091. mutex_init(&lsm_common.apr_lock);
  2092. for (; i <= LSM_MAX_SESSION_ID; i++) {
  2093. lsm_common.common_client[i].session = LSM_CONTROL_SESSION;
  2094. init_waitqueue_head(&lsm_common.common_client[i].cmd_wait);
  2095. mutex_init(&lsm_common.common_client[i].cmd_lock);
  2096. atomic_set(&lsm_common.common_client[i].cmd_state,
  2097. CMD_STATE_CLEARED);
  2098. }
  2099. if (q6lsm_init_cal_data())
  2100. pr_err("%s: could not init cal data!\n", __func__);
  2101. return 0;
  2102. }
  2103. void q6lsm_exit(void)
  2104. {
  2105. lsm_delete_cal_data();
  2106. }