q6lsm.c 72 KB

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