q6lsm.c 66 KB

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