q6lsm.c 72 KB

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