q6lsm.c 71 KB

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