q6lsm.c 74 KB

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