rtac.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  1. /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. */
  13. #include <linux/fs.h>
  14. #include <linux/module.h>
  15. #include <linux/miscdevice.h>
  16. #include <linux/slab.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/mutex.h>
  19. #include <linux/sched.h>
  20. #include <linux/msm_audio_calibration.h>
  21. #include <linux/atomic.h>
  22. #include <linux/compat.h>
  23. #include <dsp/msm_audio_ion.h>
  24. #include <dsp/rtac.h>
  25. #include <dsp/q6asm-v2.h>
  26. #include <dsp/q6afe-v2.h>
  27. #include <dsp/q6adm-v2.h>
  28. #include <dsp/apr_audio-v2.h>
  29. #include <dsp/q6common.h>
  30. #include <dsp/q6voice.h>
  31. #include "adsp_err.h"
  32. /* Max size of payload (buf size - apr header) */
  33. #define MAX_PAYLOAD_SIZE 4076
  34. #define RTAC_MAX_ACTIVE_VOICE_COMBOS 2
  35. #define RTAC_MAX_ACTIVE_POPP 8
  36. #define RTAC_BUF_SIZE 163840
  37. #define TIMEOUT_MS 1000
  38. struct rtac_cal_block_data rtac_cal[MAX_RTAC_BLOCKS] = {
  39. /* ADM_RTAC_CAL */
  40. {{RTAC_BUF_SIZE, 0, 0}, {0, 0, 0} },
  41. /* ASM_RTAC_CAL */
  42. {{RTAC_BUF_SIZE, 0, 0}, {0, 0, 0} },
  43. /* VOICE_RTAC_CAL */
  44. {{RTAC_BUF_SIZE, 0, 0}, {0, 0, 0} },
  45. /* AFE_RTAC_CAL */
  46. {{RTAC_BUF_SIZE, 0, 0}, {0, 0, 0} }
  47. };
  48. struct rtac_common_data {
  49. atomic_t usage_count;
  50. atomic_t apr_err_code;
  51. struct mutex rtac_fops_mutex;
  52. };
  53. static struct rtac_common_data rtac_common;
  54. /* APR data */
  55. struct rtac_apr_data {
  56. void *apr_handle;
  57. atomic_t cmd_state;
  58. wait_queue_head_t cmd_wait;
  59. };
  60. static struct rtac_apr_data rtac_adm_apr_data;
  61. static struct rtac_apr_data rtac_asm_apr_data[ASM_ACTIVE_STREAMS_ALLOWED + 1];
  62. static struct rtac_apr_data rtac_afe_apr_data;
  63. static struct rtac_apr_data rtac_voice_apr_data[RTAC_VOICE_MODES];
  64. /* ADM info & APR */
  65. static struct rtac_adm rtac_adm_data;
  66. static u32 *rtac_adm_buffer;
  67. /* ASM APR */
  68. static u32 *rtac_asm_buffer;
  69. static u32 *rtac_afe_buffer;
  70. /* Voice info & APR */
  71. struct rtac_voice_data_t {
  72. uint32_t tx_topology_id;
  73. uint32_t rx_topology_id;
  74. uint32_t tx_afe_topology;
  75. uint32_t rx_afe_topology;
  76. uint32_t tx_afe_port;
  77. uint32_t rx_afe_port;
  78. uint16_t cvs_handle;
  79. uint16_t cvp_handle;
  80. uint32_t tx_acdb_id;
  81. uint32_t rx_acdb_id;
  82. };
  83. struct rtac_voice {
  84. uint32_t num_of_voice_combos;
  85. struct rtac_voice_data_t voice[RTAC_MAX_ACTIVE_VOICE_COMBOS];
  86. };
  87. struct rtac_afe_user_data {
  88. uint32_t buf_size;
  89. uint32_t cmd_size;
  90. uint32_t port_id;
  91. union {
  92. struct afe_rtac_user_data_set_v2 v2_set;
  93. struct afe_rtac_user_data_set_v3 v3_set;
  94. struct afe_rtac_user_data_get_v2 v2_get;
  95. struct afe_rtac_user_data_get_v3 v3_get;
  96. };
  97. } __packed;
  98. static struct rtac_voice rtac_voice_data;
  99. static u32 *rtac_voice_buffer;
  100. static u32 voice_session_id[RTAC_MAX_ACTIVE_VOICE_COMBOS];
  101. struct mutex rtac_adm_mutex;
  102. struct mutex rtac_adm_apr_mutex;
  103. struct mutex rtac_asm_apr_mutex;
  104. struct mutex rtac_voice_mutex;
  105. struct mutex rtac_voice_apr_mutex;
  106. struct mutex rtac_afe_apr_mutex;
  107. int rtac_clear_mapping(uint32_t cal_type)
  108. {
  109. int result = 0;
  110. pr_debug("%s\n", __func__);
  111. if (cal_type >= MAX_RTAC_BLOCKS) {
  112. pr_debug("%s: invalid cal type %d\n", __func__, cal_type);
  113. result = -EINVAL;
  114. goto done;
  115. }
  116. rtac_cal[cal_type].map_data.map_handle = 0;
  117. done:
  118. return result;
  119. }
  120. int rtac_allocate_cal_buffer(uint32_t cal_type)
  121. {
  122. int result = 0;
  123. size_t len;
  124. pr_debug("%s\n", __func__);
  125. if (cal_type >= MAX_RTAC_BLOCKS) {
  126. pr_err("%s: cal_type %d is invalid!\n",
  127. __func__, cal_type);
  128. result = -EINVAL;
  129. goto done;
  130. }
  131. if (rtac_cal[cal_type].cal_data.paddr != 0) {
  132. pr_err("%s: memory already allocated! cal_type %d, paddr 0x%pK\n",
  133. __func__, cal_type, &rtac_cal[cal_type].cal_data.paddr);
  134. result = -EPERM;
  135. goto done;
  136. }
  137. result = msm_audio_ion_alloc(&rtac_cal[cal_type].map_data.dma_buf,
  138. rtac_cal[cal_type].map_data.map_size,
  139. &rtac_cal[cal_type].cal_data.paddr,
  140. &len,
  141. &rtac_cal[cal_type].cal_data.kvaddr);
  142. if (result < 0) {
  143. pr_err("%s: ION create client for RTAC failed\n",
  144. __func__);
  145. goto done;
  146. }
  147. pr_debug("%s: cal_type %d, paddr 0x%pK, kvaddr 0x%pK, map_size 0x%x\n",
  148. __func__, cal_type,
  149. &rtac_cal[cal_type].cal_data.paddr,
  150. rtac_cal[cal_type].cal_data.kvaddr,
  151. rtac_cal[cal_type].map_data.map_size);
  152. done:
  153. return result;
  154. }
  155. int rtac_free_cal_buffer(uint32_t cal_type)
  156. {
  157. int result = 0;
  158. pr_debug("%s\n", __func__);
  159. if (cal_type >= MAX_RTAC_BLOCKS) {
  160. pr_err("%s: cal_type %d is invalid!\n",
  161. __func__, cal_type);
  162. result = -EINVAL;
  163. goto done;
  164. }
  165. if (rtac_cal[cal_type].map_data.dma_buf == NULL) {
  166. pr_debug("%s: cal_type %d not allocated!\n",
  167. __func__, cal_type);
  168. goto done;
  169. }
  170. result = msm_audio_ion_free(rtac_cal[cal_type].map_data.dma_buf);
  171. if (result < 0) {
  172. pr_err("%s: ION free for RTAC failed! cal_type %d, paddr 0x%pK\n",
  173. __func__, cal_type, &rtac_cal[cal_type].cal_data.paddr);
  174. goto done;
  175. }
  176. rtac_cal[cal_type].map_data.map_handle = 0;
  177. rtac_cal[cal_type].map_data.dma_buf = NULL;
  178. rtac_cal[cal_type].cal_data.size = 0;
  179. rtac_cal[cal_type].cal_data.kvaddr = 0;
  180. rtac_cal[cal_type].cal_data.paddr = 0;
  181. done:
  182. return result;
  183. }
  184. int rtac_map_cal_buffer(uint32_t cal_type)
  185. {
  186. int result = 0;
  187. pr_debug("%s\n", __func__);
  188. if (cal_type >= MAX_RTAC_BLOCKS) {
  189. pr_err("%s: cal_type %d is invalid!\n",
  190. __func__, cal_type);
  191. result = -EINVAL;
  192. goto done;
  193. }
  194. if (rtac_cal[cal_type].map_data.map_handle != 0) {
  195. pr_err("%s: already mapped cal_type %d\n",
  196. __func__, cal_type);
  197. result = -EPERM;
  198. goto done;
  199. }
  200. if (rtac_cal[cal_type].cal_data.paddr == 0) {
  201. pr_err("%s: physical address is NULL cal_type %d\n",
  202. __func__, cal_type);
  203. result = -EPERM;
  204. goto done;
  205. }
  206. switch (cal_type) {
  207. case ADM_RTAC_CAL:
  208. result = adm_map_rtac_block(&rtac_cal[cal_type]);
  209. break;
  210. case ASM_RTAC_CAL:
  211. result = q6asm_map_rtac_block(&rtac_cal[cal_type]);
  212. break;
  213. case VOICE_RTAC_CAL:
  214. result = voc_map_rtac_block(&rtac_cal[cal_type]);
  215. break;
  216. case AFE_RTAC_CAL:
  217. result = afe_map_rtac_block(&rtac_cal[cal_type]);
  218. break;
  219. }
  220. if (result < 0) {
  221. pr_err("%s: map RTAC failed! cal_type %d\n",
  222. __func__, cal_type);
  223. goto done;
  224. }
  225. done:
  226. return result;
  227. }
  228. int rtac_unmap_cal_buffer(uint32_t cal_type)
  229. {
  230. int result = 0;
  231. pr_debug("%s\n", __func__);
  232. if (cal_type >= MAX_RTAC_BLOCKS) {
  233. pr_err("%s: cal_type %d is invalid!\n",
  234. __func__, cal_type);
  235. result = -EINVAL;
  236. goto done;
  237. }
  238. if (rtac_cal[cal_type].map_data.map_handle == 0) {
  239. pr_debug("%s: nothing to unmap cal_type %d\n",
  240. __func__, cal_type);
  241. goto done;
  242. }
  243. switch (cal_type) {
  244. case ADM_RTAC_CAL:
  245. result = adm_unmap_rtac_block(
  246. &rtac_cal[cal_type].map_data.map_handle);
  247. break;
  248. case ASM_RTAC_CAL:
  249. result = q6asm_unmap_rtac_block(
  250. &rtac_cal[cal_type].map_data.map_handle);
  251. break;
  252. case VOICE_RTAC_CAL:
  253. result = voc_unmap_rtac_block(
  254. &rtac_cal[cal_type].map_data.map_handle);
  255. break;
  256. case AFE_RTAC_CAL:
  257. result = afe_unmap_rtac_block(
  258. &rtac_cal[cal_type].map_data.map_handle);
  259. break;
  260. }
  261. if (result < 0) {
  262. pr_err("%s: unmap RTAC failed! cal_type %d\n",
  263. __func__, cal_type);
  264. goto done;
  265. }
  266. done:
  267. return result;
  268. }
  269. static int rtac_open(struct inode *inode, struct file *f)
  270. {
  271. int result = 0;
  272. pr_debug("%s\n", __func__);
  273. mutex_lock(&rtac_common.rtac_fops_mutex);
  274. atomic_inc(&rtac_common.usage_count);
  275. mutex_unlock(&rtac_common.rtac_fops_mutex);
  276. return result;
  277. }
  278. static int rtac_release(struct inode *inode, struct file *f)
  279. {
  280. int result = 0;
  281. int result2 = 0;
  282. int i;
  283. pr_debug("%s\n", __func__);
  284. mutex_lock(&rtac_common.rtac_fops_mutex);
  285. atomic_dec(&rtac_common.usage_count);
  286. pr_debug("%s: ref count %d!\n", __func__,
  287. atomic_read(&rtac_common.usage_count));
  288. if (atomic_read(&rtac_common.usage_count) > 0) {
  289. mutex_unlock(&rtac_common.rtac_fops_mutex);
  290. goto done;
  291. }
  292. for (i = 0; i < MAX_RTAC_BLOCKS; i++) {
  293. result2 = rtac_unmap_cal_buffer(i);
  294. if (result2 < 0) {
  295. pr_err("%s: unmap buffer failed! error %d!\n",
  296. __func__, result2);
  297. result = result2;
  298. }
  299. result2 = rtac_free_cal_buffer(i);
  300. if (result2 < 0) {
  301. pr_err("%s: free buffer failed! error %d!\n",
  302. __func__, result2);
  303. result = result2;
  304. }
  305. }
  306. mutex_unlock(&rtac_common.rtac_fops_mutex);
  307. done:
  308. return result;
  309. }
  310. /* ADM Info */
  311. void add_popp(u32 dev_idx, u32 port_id, u32 popp_id)
  312. {
  313. u32 i = 0;
  314. for (; i < rtac_adm_data.device[dev_idx].num_of_popp; i++)
  315. if (rtac_adm_data.device[dev_idx].popp[i].popp == popp_id)
  316. goto done;
  317. if (rtac_adm_data.device[dev_idx].num_of_popp ==
  318. RTAC_MAX_ACTIVE_POPP) {
  319. pr_err("%s, Max POPP!\n", __func__);
  320. goto done;
  321. }
  322. rtac_adm_data.device[dev_idx].popp[
  323. rtac_adm_data.device[dev_idx].num_of_popp].popp = popp_id;
  324. rtac_adm_data.device[dev_idx].popp[
  325. rtac_adm_data.device[dev_idx].num_of_popp].popp_topology =
  326. q6asm_get_asm_topology(popp_id);
  327. rtac_adm_data.device[dev_idx].popp[
  328. rtac_adm_data.device[dev_idx].num_of_popp++].app_type =
  329. q6asm_get_asm_app_type(popp_id);
  330. pr_debug("%s: popp_id = %d, popp topology = 0x%x, popp app type = 0x%x\n",
  331. __func__,
  332. rtac_adm_data.device[dev_idx].popp[
  333. rtac_adm_data.device[dev_idx].num_of_popp - 1].popp,
  334. rtac_adm_data.device[dev_idx].popp[
  335. rtac_adm_data.device[dev_idx].num_of_popp - 1].popp_topology,
  336. rtac_adm_data.device[dev_idx].popp[
  337. rtac_adm_data.device[dev_idx].num_of_popp - 1].app_type);
  338. done:
  339. return;
  340. }
  341. void rtac_update_afe_topology(u32 port_id)
  342. {
  343. u32 i = 0;
  344. mutex_lock(&rtac_adm_mutex);
  345. for (i = 0; i < rtac_adm_data.num_of_dev; i++) {
  346. if (rtac_adm_data.device[i].afe_port == port_id) {
  347. rtac_adm_data.device[i].afe_topology =
  348. afe_get_topology(port_id);
  349. pr_debug("%s: port_id = 0x%x topology_id = 0x%x copp_id = %d\n",
  350. __func__, port_id,
  351. rtac_adm_data.device[i].afe_topology,
  352. rtac_adm_data.device[i].copp);
  353. }
  354. }
  355. mutex_unlock(&rtac_adm_mutex);
  356. }
  357. void rtac_add_adm_device(u32 port_id, u32 copp_id, u32 path_id, u32 popp_id,
  358. u32 app_type, u32 acdb_id)
  359. {
  360. u32 i = 0;
  361. pr_debug("%s: num rtac devices %d port_id = %d, copp_id = %d\n",
  362. __func__, rtac_adm_data.num_of_dev, port_id, copp_id);
  363. mutex_lock(&rtac_adm_mutex);
  364. if (rtac_adm_data.num_of_dev == RTAC_MAX_ACTIVE_DEVICES) {
  365. pr_err("%s, Can't add anymore RTAC devices!\n", __func__);
  366. goto done;
  367. }
  368. /* Check if device already added */
  369. if (rtac_adm_data.num_of_dev != 0) {
  370. for (; i < rtac_adm_data.num_of_dev; i++) {
  371. if (rtac_adm_data.device[i].afe_port == port_id &&
  372. rtac_adm_data.device[i].copp == copp_id) {
  373. add_popp(i, port_id, popp_id);
  374. goto done;
  375. }
  376. if (rtac_adm_data.device[i].num_of_popp ==
  377. RTAC_MAX_ACTIVE_POPP) {
  378. pr_err("%s, Max POPP!\n", __func__);
  379. goto done;
  380. }
  381. }
  382. }
  383. /* Add device */
  384. rtac_adm_data.num_of_dev++;
  385. rtac_adm_data.device[i].topology_id =
  386. adm_get_topology_for_port_from_copp_id(port_id, copp_id);
  387. rtac_adm_data.device[i].afe_topology =
  388. afe_get_topology(port_id);
  389. rtac_adm_data.device[i].afe_port = port_id;
  390. rtac_adm_data.device[i].copp = copp_id;
  391. rtac_adm_data.device[i].app_type = app_type;
  392. rtac_adm_data.device[i].acdb_dev_id = acdb_id;
  393. rtac_adm_data.device[i].popp[
  394. rtac_adm_data.device[i].num_of_popp].popp = popp_id;
  395. rtac_adm_data.device[i].popp[
  396. rtac_adm_data.device[i].num_of_popp].popp_topology =
  397. q6asm_get_asm_topology(popp_id);
  398. rtac_adm_data.device[i].popp[
  399. rtac_adm_data.device[i].num_of_popp++].app_type =
  400. q6asm_get_asm_app_type(popp_id);
  401. pr_debug("%s: topology = 0x%x, afe_topology = 0x%x, port_id = %d, copp_id = %d, app id = 0x%x, acdb id = %d, popp_id = %d, popp topology = 0x%x, popp app type = 0x%x\n",
  402. __func__,
  403. rtac_adm_data.device[i].topology_id,
  404. rtac_adm_data.device[i].afe_topology,
  405. rtac_adm_data.device[i].afe_port,
  406. rtac_adm_data.device[i].copp,
  407. rtac_adm_data.device[i].app_type,
  408. rtac_adm_data.device[i].acdb_dev_id,
  409. rtac_adm_data.device[i].popp[
  410. rtac_adm_data.device[i].num_of_popp - 1].popp,
  411. rtac_adm_data.device[i].popp[
  412. rtac_adm_data.device[i].num_of_popp - 1].popp_topology,
  413. rtac_adm_data.device[i].popp[
  414. rtac_adm_data.device[i].num_of_popp - 1].app_type);
  415. done:
  416. mutex_unlock(&rtac_adm_mutex);
  417. }
  418. static void shift_adm_devices(u32 dev_idx)
  419. {
  420. for (; dev_idx < rtac_adm_data.num_of_dev; dev_idx++) {
  421. memcpy(&rtac_adm_data.device[dev_idx],
  422. &rtac_adm_data.device[dev_idx + 1],
  423. sizeof(rtac_adm_data.device[dev_idx]));
  424. memset(&rtac_adm_data.device[dev_idx + 1], 0,
  425. sizeof(rtac_adm_data.device[dev_idx]));
  426. }
  427. }
  428. static void shift_popp(u32 copp_idx, u32 popp_idx)
  429. {
  430. for (; popp_idx < rtac_adm_data.device[copp_idx].num_of_popp;
  431. popp_idx++) {
  432. memcpy(&rtac_adm_data.device[copp_idx].popp[popp_idx].popp,
  433. &rtac_adm_data.device[copp_idx].popp[popp_idx + 1].
  434. popp, sizeof(uint32_t));
  435. memcpy(&rtac_adm_data.device[copp_idx].popp[popp_idx].
  436. popp_topology,
  437. &rtac_adm_data.device[copp_idx].popp[popp_idx + 1].
  438. popp_topology,
  439. sizeof(uint32_t));
  440. memset(&rtac_adm_data.device[copp_idx].popp[popp_idx + 1].
  441. popp, 0, sizeof(uint32_t));
  442. memset(&rtac_adm_data.device[copp_idx].popp[popp_idx + 1].
  443. popp_topology, 0, sizeof(uint32_t));
  444. }
  445. }
  446. void rtac_remove_adm_device(u32 port_id, u32 copp_id)
  447. {
  448. s32 i;
  449. pr_debug("%s: num rtac devices %d port_id = %d, copp_id = %d\n",
  450. __func__, rtac_adm_data.num_of_dev, port_id, copp_id);
  451. mutex_lock(&rtac_adm_mutex);
  452. /* look for device */
  453. for (i = 0; i < rtac_adm_data.num_of_dev; i++) {
  454. if (rtac_adm_data.device[i].afe_port == port_id &&
  455. rtac_adm_data.device[i].copp == copp_id) {
  456. memset(&rtac_adm_data.device[i], 0,
  457. sizeof(rtac_adm_data.device[i]));
  458. rtac_adm_data.num_of_dev--;
  459. if (rtac_adm_data.num_of_dev >= 1) {
  460. shift_adm_devices(i);
  461. break;
  462. }
  463. }
  464. }
  465. mutex_unlock(&rtac_adm_mutex);
  466. }
  467. void rtac_remove_popp_from_adm_devices(u32 popp_id)
  468. {
  469. s32 i, j;
  470. pr_debug("%s: popp_id = %d\n", __func__, popp_id);
  471. mutex_lock(&rtac_adm_mutex);
  472. for (i = 0; i < rtac_adm_data.num_of_dev; i++) {
  473. for (j = 0; j < rtac_adm_data.device[i].num_of_popp; j++) {
  474. if (rtac_adm_data.device[i].popp[j].popp ==
  475. popp_id) {
  476. rtac_adm_data.device[i].popp[j].popp = 0;
  477. rtac_adm_data.device[i].popp[j].
  478. popp_topology = 0;
  479. rtac_adm_data.device[i].num_of_popp--;
  480. shift_popp(i, j);
  481. }
  482. }
  483. }
  484. mutex_unlock(&rtac_adm_mutex);
  485. }
  486. /* Voice Info */
  487. static void set_rtac_voice_data(int idx, u32 cvs_handle, u32 cvp_handle,
  488. u32 rx_afe_port, u32 tx_afe_port,
  489. u32 rx_acdb_id, u32 tx_acdb_id,
  490. u32 session_id)
  491. {
  492. rtac_voice_data.voice[idx].tx_topology_id =
  493. voice_get_topology(CVP_VOC_TX_TOPOLOGY_CAL);
  494. rtac_voice_data.voice[idx].rx_topology_id =
  495. voice_get_topology(CVP_VOC_RX_TOPOLOGY_CAL);
  496. rtac_voice_data.voice[idx].tx_afe_topology =
  497. afe_get_topology(tx_afe_port);
  498. rtac_voice_data.voice[idx].rx_afe_topology =
  499. afe_get_topology(rx_afe_port);
  500. rtac_voice_data.voice[idx].tx_afe_port = tx_afe_port;
  501. rtac_voice_data.voice[idx].rx_afe_port = rx_afe_port;
  502. rtac_voice_data.voice[idx].tx_acdb_id = tx_acdb_id;
  503. rtac_voice_data.voice[idx].rx_acdb_id = rx_acdb_id;
  504. rtac_voice_data.voice[idx].cvs_handle = cvs_handle;
  505. rtac_voice_data.voice[idx].cvp_handle = cvp_handle;
  506. pr_debug("%s\n%s: %x\n%s: %d %s: %d\n%s: %d %s: %d\n %s: %d\n %s: %d\n%s: %d %s: %d\n%s",
  507. "<---- Voice Data Info ---->", "Session id", session_id,
  508. "cvs_handle", cvs_handle, "cvp_handle", cvp_handle,
  509. "rx_afe_topology", rtac_voice_data.voice[idx].rx_afe_topology,
  510. "tx_afe_topology", rtac_voice_data.voice[idx].tx_afe_topology,
  511. "rx_afe_port", rx_afe_port, "tx_afe_port", tx_afe_port,
  512. "rx_acdb_id", rx_acdb_id, "tx_acdb_id", tx_acdb_id,
  513. "<-----------End----------->");
  514. /* Store session ID for voice RTAC */
  515. voice_session_id[idx] = session_id;
  516. }
  517. void rtac_add_voice(u32 cvs_handle, u32 cvp_handle, u32 rx_afe_port,
  518. u32 tx_afe_port, u32 rx_acdb_id, u32 tx_acdb_id,
  519. u32 session_id)
  520. {
  521. u32 i = 0;
  522. pr_debug("%s\n", __func__);
  523. mutex_lock(&rtac_voice_mutex);
  524. if (rtac_voice_data.num_of_voice_combos ==
  525. RTAC_MAX_ACTIVE_VOICE_COMBOS) {
  526. pr_err("%s, Can't add anymore RTAC devices!\n", __func__);
  527. goto done;
  528. }
  529. /* Check if device already added */
  530. if (rtac_voice_data.num_of_voice_combos != 0) {
  531. for (; i < rtac_voice_data.num_of_voice_combos; i++) {
  532. if (rtac_voice_data.voice[i].cvs_handle ==
  533. cvs_handle) {
  534. set_rtac_voice_data(i, cvs_handle, cvp_handle,
  535. rx_afe_port, tx_afe_port, rx_acdb_id,
  536. tx_acdb_id, session_id);
  537. goto done;
  538. }
  539. }
  540. }
  541. /* Add device */
  542. rtac_voice_data.num_of_voice_combos++;
  543. set_rtac_voice_data(i, cvs_handle, cvp_handle,
  544. rx_afe_port, tx_afe_port,
  545. rx_acdb_id, tx_acdb_id,
  546. session_id);
  547. done:
  548. mutex_unlock(&rtac_voice_mutex);
  549. }
  550. static void shift_voice_devices(u32 idx)
  551. {
  552. for (; idx < rtac_voice_data.num_of_voice_combos - 1; idx++) {
  553. memcpy(&rtac_voice_data.voice[idx],
  554. &rtac_voice_data.voice[idx + 1],
  555. sizeof(rtac_voice_data.voice[idx]));
  556. voice_session_id[idx] = voice_session_id[idx + 1];
  557. }
  558. }
  559. void rtac_remove_voice(u32 cvs_handle)
  560. {
  561. u32 i = 0;
  562. pr_debug("%s\n", __func__);
  563. mutex_lock(&rtac_voice_mutex);
  564. /* look for device */
  565. for (i = 0; i < rtac_voice_data.num_of_voice_combos; i++) {
  566. if (rtac_voice_data.voice[i].cvs_handle == cvs_handle) {
  567. shift_voice_devices(i);
  568. rtac_voice_data.num_of_voice_combos--;
  569. memset(&rtac_voice_data.voice[
  570. rtac_voice_data.num_of_voice_combos], 0,
  571. sizeof(rtac_voice_data.voice
  572. [rtac_voice_data.num_of_voice_combos]));
  573. voice_session_id[rtac_voice_data.num_of_voice_combos]
  574. = 0;
  575. break;
  576. }
  577. }
  578. mutex_unlock(&rtac_voice_mutex);
  579. }
  580. static u32 get_voice_session_id_cvs(u32 cvs_handle)
  581. {
  582. u32 i;
  583. for (i = 0; i < rtac_voice_data.num_of_voice_combos; i++) {
  584. if (rtac_voice_data.voice[i].cvs_handle == cvs_handle)
  585. return voice_session_id[i];
  586. }
  587. pr_err("%s: No voice index for CVS handle %d found returning 0\n",
  588. __func__, cvs_handle);
  589. return 0;
  590. }
  591. static u32 get_voice_session_id_cvp(u32 cvp_handle)
  592. {
  593. u32 i;
  594. for (i = 0; i < rtac_voice_data.num_of_voice_combos; i++) {
  595. if (rtac_voice_data.voice[i].cvp_handle == cvp_handle)
  596. return voice_session_id[i];
  597. }
  598. pr_err("%s: No voice index for CVP handle %d found returning 0\n",
  599. __func__, cvp_handle);
  600. return 0;
  601. }
  602. static int get_voice_index(u32 mode, u32 handle)
  603. {
  604. if (mode == RTAC_CVP)
  605. return voice_get_idx_for_session(
  606. get_voice_session_id_cvp(handle));
  607. if (mode == RTAC_CVS)
  608. return voice_get_idx_for_session(
  609. get_voice_session_id_cvs(handle));
  610. pr_err("%s: Invalid mode %d, returning 0\n",
  611. __func__, mode);
  612. return 0;
  613. }
  614. /* ADM APR */
  615. void rtac_set_adm_handle(void *handle)
  616. {
  617. pr_debug("%s: handle = %pK\n", __func__, handle);
  618. mutex_lock(&rtac_adm_apr_mutex);
  619. rtac_adm_apr_data.apr_handle = handle;
  620. mutex_unlock(&rtac_adm_apr_mutex);
  621. }
  622. bool rtac_make_adm_callback(uint32_t *payload, u32 payload_size)
  623. {
  624. pr_debug("%s:cmd_state = %d\n", __func__,
  625. atomic_read(&rtac_adm_apr_data.cmd_state));
  626. if (atomic_read(&rtac_adm_apr_data.cmd_state) != 1)
  627. return false;
  628. pr_debug("%s\n", __func__);
  629. if (payload_size == sizeof(uint32_t))
  630. atomic_set(&rtac_common.apr_err_code, payload[0]);
  631. else if (payload_size == (2*sizeof(uint32_t)))
  632. atomic_set(&rtac_common.apr_err_code, payload[1]);
  633. atomic_set(&rtac_adm_apr_data.cmd_state, 0);
  634. wake_up(&rtac_adm_apr_data.cmd_wait);
  635. return true;
  636. }
  637. int send_adm_apr(void *buf, u32 opcode)
  638. {
  639. s32 result;
  640. u32 user_buf_size = 0;
  641. u32 bytes_returned = 0;
  642. u32 copp_id;
  643. u32 payload_size;
  644. u32 data_size = 0;
  645. int copp_idx;
  646. int port_idx;
  647. struct apr_hdr adm_params;
  648. pr_debug("%s\n", __func__);
  649. if (rtac_cal[ADM_RTAC_CAL].map_data.dma_buf == NULL) {
  650. result = rtac_allocate_cal_buffer(ADM_RTAC_CAL);
  651. if (result < 0) {
  652. pr_err("%s: allocate buffer failed!",
  653. __func__);
  654. goto done;
  655. }
  656. }
  657. if (rtac_cal[ADM_RTAC_CAL].map_data.map_handle == 0) {
  658. result = rtac_map_cal_buffer(ADM_RTAC_CAL);
  659. if (result < 0) {
  660. pr_err("%s: map buffer failed!",
  661. __func__);
  662. goto done;
  663. }
  664. }
  665. if (copy_from_user(&user_buf_size, (void *)buf,
  666. sizeof(user_buf_size))) {
  667. pr_err("%s: Copy from user failed! buf = 0x%pK\n",
  668. __func__, buf);
  669. goto done;
  670. }
  671. if (user_buf_size <= 0) {
  672. pr_err("%s: Invalid buffer size = %d\n",
  673. __func__, user_buf_size);
  674. goto done;
  675. }
  676. if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
  677. pr_err("%s: Could not copy payload size from user buffer\n",
  678. __func__);
  679. goto done;
  680. }
  681. if (copy_from_user(&copp_id, buf + 2 * sizeof(u32), sizeof(u32))) {
  682. pr_err("%s: Could not copy port id from user buffer\n",
  683. __func__);
  684. goto done;
  685. }
  686. if (adm_get_indexes_from_copp_id(copp_id, &copp_idx, &port_idx) != 0) {
  687. pr_err("%s: Copp Id-%d is not active\n", __func__, copp_id);
  688. goto done;
  689. }
  690. mutex_lock(&rtac_adm_apr_mutex);
  691. if (rtac_adm_apr_data.apr_handle == NULL) {
  692. pr_err("%s: APR not initialized\n", __func__);
  693. result = -EINVAL;
  694. goto err;
  695. }
  696. switch (opcode) {
  697. case ADM_CMD_SET_PP_PARAMS_V5:
  698. case ADM_CMD_SET_PP_PARAMS_V6:
  699. /* set payload size to in-band payload */
  700. /* set data size to actual out of band payload size */
  701. data_size = payload_size - 4 * sizeof(u32);
  702. if (data_size > rtac_cal[ADM_RTAC_CAL].map_data.map_size) {
  703. pr_err("%s: Invalid data size = %d\n",
  704. __func__, data_size);
  705. result = -EINVAL;
  706. goto err;
  707. }
  708. payload_size = 4 * sizeof(u32);
  709. /* Copy buffer to out-of-band payload */
  710. if (copy_from_user((void *)
  711. rtac_cal[ADM_RTAC_CAL].cal_data.kvaddr,
  712. buf + 7 * sizeof(u32), data_size)) {
  713. pr_err("%s: Could not copy payload from user buffer\n",
  714. __func__);
  715. result = -EFAULT;
  716. goto err;
  717. }
  718. /* set payload size in packet */
  719. rtac_adm_buffer[8] = data_size;
  720. break;
  721. case ADM_CMD_GET_PP_PARAMS_V5:
  722. case ADM_CMD_GET_PP_PARAMS_V6:
  723. if (payload_size > MAX_PAYLOAD_SIZE) {
  724. pr_err("%s: Invalid payload size = %d\n",
  725. __func__, payload_size);
  726. result = -EINVAL;
  727. goto err;
  728. }
  729. /* Copy buffer to in-band payload */
  730. if (copy_from_user(rtac_adm_buffer +
  731. sizeof(adm_params)/sizeof(u32),
  732. buf + 3 * sizeof(u32), payload_size)) {
  733. pr_err("%s: Could not copy payload from user buffer\n",
  734. __func__);
  735. result = -EFAULT;
  736. goto err;
  737. }
  738. break;
  739. default:
  740. pr_err("%s: Invalid opcode %d\n", __func__, opcode);
  741. result = -EINVAL;
  742. goto err;
  743. }
  744. /* Pack header */
  745. adm_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  746. APR_HDR_LEN(20), APR_PKT_VER);
  747. adm_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  748. payload_size);
  749. adm_params.src_svc = APR_SVC_ADM;
  750. adm_params.src_domain = APR_DOMAIN_APPS;
  751. adm_params.src_port = copp_id;
  752. adm_params.dest_svc = APR_SVC_ADM;
  753. adm_params.dest_domain = APR_DOMAIN_ADSP;
  754. adm_params.dest_port = copp_id;
  755. adm_params.token = port_idx << 16 | copp_idx;
  756. adm_params.opcode = opcode;
  757. /* fill for out-of-band */
  758. rtac_adm_buffer[5] =
  759. lower_32_bits(rtac_cal[ADM_RTAC_CAL].cal_data.paddr);
  760. rtac_adm_buffer[6] =
  761. msm_audio_populate_upper_32_bits(
  762. rtac_cal[ADM_RTAC_CAL].cal_data.paddr);
  763. rtac_adm_buffer[7] = rtac_cal[ADM_RTAC_CAL].map_data.map_handle;
  764. memcpy(rtac_adm_buffer, &adm_params, sizeof(adm_params));
  765. atomic_set(&rtac_adm_apr_data.cmd_state, 1);
  766. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
  767. __func__, opcode,
  768. &rtac_cal[ADM_RTAC_CAL].cal_data.paddr);
  769. result = apr_send_pkt(rtac_adm_apr_data.apr_handle,
  770. (uint32_t *)rtac_adm_buffer);
  771. if (result < 0) {
  772. pr_err("%s: Set params failed copp = %d\n", __func__, copp_id);
  773. goto err;
  774. }
  775. /* Wait for the callback */
  776. result = wait_event_timeout(rtac_adm_apr_data.cmd_wait,
  777. (atomic_read(&rtac_adm_apr_data.cmd_state) == 0),
  778. msecs_to_jiffies(TIMEOUT_MS));
  779. if (!result) {
  780. pr_err("%s: Set params timed out copp = %d\n", __func__,
  781. copp_id);
  782. goto err;
  783. }
  784. if (atomic_read(&rtac_common.apr_err_code)) {
  785. pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
  786. __func__, adsp_err_get_err_str(atomic_read(
  787. &rtac_common.apr_err_code)),
  788. opcode);
  789. result = adsp_err_get_lnx_err_code(
  790. atomic_read(
  791. &rtac_common.apr_err_code));
  792. goto err;
  793. }
  794. if (opcode == ADM_CMD_GET_PP_PARAMS_V5) {
  795. bytes_returned = ((u32 *)rtac_cal[ADM_RTAC_CAL].cal_data.
  796. kvaddr)[2] + 3 * sizeof(u32);
  797. } else if (opcode == ADM_CMD_GET_PP_PARAMS_V6) {
  798. bytes_returned =
  799. ((u32 *) rtac_cal[ADM_RTAC_CAL].cal_data.kvaddr)[3] +
  800. 4 * sizeof(u32);
  801. } else {
  802. bytes_returned = data_size;
  803. goto unlock;
  804. }
  805. if (bytes_returned > rtac_cal[ADM_RTAC_CAL].map_data.map_size) {
  806. pr_err("%s: Invalid data size = %d\n", __func__,
  807. bytes_returned);
  808. result = -EINVAL;
  809. goto err;
  810. }
  811. if (bytes_returned > user_buf_size) {
  812. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  813. __func__, user_buf_size, bytes_returned);
  814. result = -EINVAL;
  815. goto err;
  816. }
  817. if (copy_to_user((void __user *) buf,
  818. rtac_cal[ADM_RTAC_CAL].cal_data.kvaddr,
  819. bytes_returned)) {
  820. pr_err("%s: Could not copy buffer to user,size = %d\n",
  821. __func__, bytes_returned);
  822. result = -EFAULT;
  823. goto err;
  824. }
  825. unlock:
  826. mutex_unlock(&rtac_adm_apr_mutex);
  827. done:
  828. return bytes_returned;
  829. err:
  830. mutex_unlock(&rtac_adm_apr_mutex);
  831. return result;
  832. }
  833. /* ASM APR */
  834. void rtac_set_asm_handle(u32 session_id, void *handle)
  835. {
  836. pr_debug("%s\n", __func__);
  837. mutex_lock(&rtac_asm_apr_mutex);
  838. rtac_asm_apr_data[session_id].apr_handle = handle;
  839. mutex_unlock(&rtac_asm_apr_mutex);
  840. }
  841. bool rtac_make_asm_callback(u32 session_id, uint32_t *payload,
  842. u32 payload_size)
  843. {
  844. if (atomic_read(&rtac_asm_apr_data[session_id].cmd_state) != 1)
  845. return false;
  846. pr_debug("%s\n", __func__);
  847. if (payload_size == sizeof(uint32_t))
  848. atomic_set(&rtac_common.apr_err_code, payload[0]);
  849. else if (payload_size == (2*sizeof(uint32_t)))
  850. atomic_set(&rtac_common.apr_err_code, payload[1]);
  851. atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 0);
  852. wake_up(&rtac_asm_apr_data[session_id].cmd_wait);
  853. return true;
  854. }
  855. int send_rtac_asm_apr(void *buf, u32 opcode)
  856. {
  857. s32 result;
  858. u32 user_buf_size = 0;
  859. u32 bytes_returned = 0;
  860. u32 session_id = 0;
  861. u32 payload_size;
  862. u32 data_size = 0;
  863. struct apr_hdr asm_params;
  864. pr_debug("%s\n", __func__);
  865. if (rtac_cal[ASM_RTAC_CAL].map_data.dma_buf == NULL) {
  866. result = rtac_allocate_cal_buffer(ASM_RTAC_CAL);
  867. if (result < 0) {
  868. pr_err("%s: allocate buffer failed!",
  869. __func__);
  870. goto done;
  871. }
  872. }
  873. if (rtac_cal[ASM_RTAC_CAL].map_data.map_handle == 0) {
  874. result = rtac_map_cal_buffer(ASM_RTAC_CAL);
  875. if (result < 0) {
  876. pr_err("%s: map buffer failed!",
  877. __func__);
  878. goto done;
  879. }
  880. }
  881. if (copy_from_user(&user_buf_size, (void *)buf,
  882. sizeof(user_buf_size))) {
  883. pr_err("%s: Copy from user failed! buf = 0x%pK\n",
  884. __func__, buf);
  885. goto done;
  886. }
  887. if (user_buf_size <= 0) {
  888. pr_err("%s: Invalid buffer size = %d\n",
  889. __func__, user_buf_size);
  890. goto done;
  891. }
  892. if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
  893. pr_err("%s: Could not copy payload size from user buffer\n",
  894. __func__);
  895. goto done;
  896. }
  897. if (copy_from_user(&session_id, buf + 2 * sizeof(u32), sizeof(u32))) {
  898. pr_err("%s: Could not copy session id from user buffer\n",
  899. __func__);
  900. goto done;
  901. }
  902. if (session_id >= (ASM_ACTIVE_STREAMS_ALLOWED + 1)) {
  903. pr_err("%s: Invalid Session = %d\n", __func__, session_id);
  904. goto done;
  905. }
  906. mutex_lock(&rtac_asm_apr_mutex);
  907. if (rtac_asm_apr_data[session_id].apr_handle == NULL) {
  908. pr_err("%s: APR not initialized\n", __func__);
  909. result = -EINVAL;
  910. goto err;
  911. }
  912. switch (opcode) {
  913. case ASM_STREAM_CMD_SET_PP_PARAMS_V2:
  914. case ASM_STREAM_CMD_SET_PP_PARAMS_V3:
  915. /* set payload size to in-band payload */
  916. /* set data size to actual out of band payload size */
  917. data_size = payload_size - 4 * sizeof(u32);
  918. if (data_size > rtac_cal[ASM_RTAC_CAL].map_data.map_size) {
  919. pr_err("%s: Invalid data size = %d\n",
  920. __func__, data_size);
  921. result = -EINVAL;
  922. goto err;
  923. }
  924. payload_size = 4 * sizeof(u32);
  925. /* Copy buffer to out-of-band payload */
  926. if (copy_from_user((void *)
  927. rtac_cal[ASM_RTAC_CAL].cal_data.kvaddr,
  928. buf + 7 * sizeof(u32), data_size)) {
  929. pr_err("%s: Could not copy payload from user buffer\n",
  930. __func__);
  931. result = -EFAULT;
  932. goto err;
  933. }
  934. /* set payload size in packet */
  935. rtac_asm_buffer[8] = data_size;
  936. break;
  937. case ASM_STREAM_CMD_GET_PP_PARAMS_V2:
  938. case ASM_STREAM_CMD_GET_PP_PARAMS_V3:
  939. if (payload_size > MAX_PAYLOAD_SIZE) {
  940. pr_err("%s: Invalid payload size = %d\n",
  941. __func__, payload_size);
  942. result = -EINVAL;
  943. goto err;
  944. }
  945. /* Copy buffer to in-band payload */
  946. if (copy_from_user(rtac_asm_buffer +
  947. sizeof(asm_params)/sizeof(u32),
  948. buf + 3 * sizeof(u32), payload_size)) {
  949. pr_err("%s: Could not copy payload from user buffer\n",
  950. __func__);
  951. result = -EFAULT;
  952. goto err;
  953. }
  954. break;
  955. default:
  956. pr_err("%s: Invalid opcode %d\n", __func__, opcode);
  957. result = -EINVAL;
  958. goto err;
  959. }
  960. /* Pack header */
  961. asm_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  962. APR_HDR_LEN(20), APR_PKT_VER);
  963. asm_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  964. payload_size);
  965. asm_params.src_svc = q6asm_get_apr_service_id(session_id);
  966. asm_params.src_domain = APR_DOMAIN_APPS;
  967. asm_params.src_port = (session_id << 8) | 0x0001;
  968. asm_params.dest_svc = APR_SVC_ASM;
  969. asm_params.dest_domain = APR_DOMAIN_ADSP;
  970. asm_params.dest_port = (session_id << 8) | 0x0001;
  971. asm_params.token = session_id;
  972. asm_params.opcode = opcode;
  973. /* fill for out-of-band */
  974. rtac_asm_buffer[5] =
  975. lower_32_bits(rtac_cal[ASM_RTAC_CAL].cal_data.paddr);
  976. rtac_asm_buffer[6] =
  977. msm_audio_populate_upper_32_bits(
  978. rtac_cal[ASM_RTAC_CAL].cal_data.paddr);
  979. rtac_asm_buffer[7] = rtac_cal[ASM_RTAC_CAL].map_data.map_handle;
  980. memcpy(rtac_asm_buffer, &asm_params, sizeof(asm_params));
  981. atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 1);
  982. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
  983. __func__, opcode,
  984. &rtac_cal[ASM_RTAC_CAL].cal_data.paddr);
  985. result = apr_send_pkt(rtac_asm_apr_data[session_id].apr_handle,
  986. (uint32_t *)rtac_asm_buffer);
  987. if (result < 0) {
  988. pr_err("%s: Set params failed session = %d\n",
  989. __func__, session_id);
  990. goto err;
  991. }
  992. /* Wait for the callback */
  993. result = wait_event_timeout(rtac_asm_apr_data[session_id].cmd_wait,
  994. (atomic_read(&rtac_asm_apr_data[session_id].cmd_state) == 0),
  995. 5 * HZ);
  996. if (!result) {
  997. pr_err("%s: Set params timed out session = %d\n",
  998. __func__, session_id);
  999. goto err;
  1000. }
  1001. if (atomic_read(&rtac_common.apr_err_code)) {
  1002. pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
  1003. __func__, adsp_err_get_err_str(atomic_read(
  1004. &rtac_common.apr_err_code)),
  1005. opcode);
  1006. result = adsp_err_get_lnx_err_code(
  1007. atomic_read(
  1008. &rtac_common.apr_err_code));
  1009. goto err;
  1010. }
  1011. if (opcode == ASM_STREAM_CMD_GET_PP_PARAMS_V2) {
  1012. bytes_returned = ((u32 *)rtac_cal[ASM_RTAC_CAL].cal_data.
  1013. kvaddr)[2] + 3 * sizeof(u32);
  1014. } else if (opcode == ASM_STREAM_CMD_GET_PP_PARAMS_V3) {
  1015. bytes_returned =
  1016. ((u32 *) rtac_cal[ASM_RTAC_CAL].cal_data.kvaddr)[3] +
  1017. 4 * sizeof(u32);
  1018. } else {
  1019. bytes_returned = data_size;
  1020. goto unlock;
  1021. }
  1022. if (bytes_returned > rtac_cal[ASM_RTAC_CAL].map_data.map_size) {
  1023. pr_err("%s: Invalid data size = %d\n", __func__,
  1024. bytes_returned);
  1025. result = -EINVAL;
  1026. goto err;
  1027. }
  1028. if (bytes_returned > user_buf_size) {
  1029. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  1030. __func__, user_buf_size, bytes_returned);
  1031. result = -EINVAL;
  1032. goto err;
  1033. }
  1034. if (copy_to_user((void __user *) buf,
  1035. rtac_cal[ASM_RTAC_CAL].cal_data.kvaddr,
  1036. bytes_returned)) {
  1037. pr_err("%s: Could not copy buffer to user,size = %d\n",
  1038. __func__, bytes_returned);
  1039. result = -EFAULT;
  1040. goto err;
  1041. }
  1042. unlock:
  1043. mutex_unlock(&rtac_asm_apr_mutex);
  1044. done:
  1045. return bytes_returned;
  1046. err:
  1047. mutex_unlock(&rtac_asm_apr_mutex);
  1048. return result;
  1049. }
  1050. /* AFE APR */
  1051. void rtac_set_afe_handle(void *handle)
  1052. {
  1053. mutex_lock(&rtac_afe_apr_mutex);
  1054. rtac_afe_apr_data.apr_handle = handle;
  1055. mutex_unlock(&rtac_afe_apr_mutex);
  1056. }
  1057. bool rtac_make_afe_callback(uint32_t *payload, uint32_t payload_size)
  1058. {
  1059. pr_debug("%s:cmd_state = %d\n", __func__,
  1060. atomic_read(&rtac_afe_apr_data.cmd_state));
  1061. if (atomic_read(&rtac_afe_apr_data.cmd_state) != 1)
  1062. return false;
  1063. if (payload_size == sizeof(uint32_t))
  1064. atomic_set(&rtac_common.apr_err_code, payload[0]);
  1065. else if (payload_size == (2*sizeof(uint32_t)))
  1066. atomic_set(&rtac_common.apr_err_code, payload[1]);
  1067. atomic_set(&rtac_afe_apr_data.cmd_state, 0);
  1068. wake_up(&rtac_afe_apr_data.cmd_wait);
  1069. return true;
  1070. }
  1071. static int fill_afe_apr_hdr(struct apr_hdr *apr_hdr, uint32_t port,
  1072. uint32_t opcode, uint32_t apr_msg_size)
  1073. {
  1074. if (apr_hdr == NULL) {
  1075. pr_err("%s: invalid APR pointer", __func__);
  1076. return -EINVAL;
  1077. }
  1078. apr_hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1079. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1080. apr_hdr->pkt_size = apr_msg_size;
  1081. apr_hdr->src_svc = APR_SVC_AFE;
  1082. apr_hdr->src_domain = APR_DOMAIN_APPS;
  1083. apr_hdr->src_port = 0;
  1084. apr_hdr->dest_svc = APR_SVC_AFE;
  1085. apr_hdr->dest_domain = APR_DOMAIN_ADSP;
  1086. apr_hdr->dest_port = 0;
  1087. apr_hdr->token = port;
  1088. apr_hdr->opcode = opcode;
  1089. return 0;
  1090. }
  1091. static int send_rtac_afe_apr(void __user *buf, uint32_t opcode)
  1092. {
  1093. int32_t result;
  1094. uint32_t bytes_returned = 0;
  1095. uint32_t payload_size = 0;
  1096. uint32_t port_index = 0;
  1097. uint32_t *afe_cmd = NULL;
  1098. uint32_t apr_msg_size = 0;
  1099. struct rtac_afe_user_data user_afe_buf;
  1100. struct mem_mapping_hdr *mem_hdr = NULL;
  1101. struct param_hdr_v1 *get_resp_v2;
  1102. struct param_hdr_v3 *get_resp_v3;
  1103. pr_debug("%s\n", __func__);
  1104. if (rtac_cal[AFE_RTAC_CAL].map_data.dma_buf == NULL) {
  1105. result = rtac_allocate_cal_buffer(AFE_RTAC_CAL);
  1106. if (result < 0) {
  1107. pr_err("%s: allocate buffer failed! ret = %d\n",
  1108. __func__, result);
  1109. goto done;
  1110. }
  1111. }
  1112. if (rtac_cal[AFE_RTAC_CAL].map_data.map_handle == 0) {
  1113. result = rtac_map_cal_buffer(AFE_RTAC_CAL);
  1114. if (result < 0) {
  1115. pr_err("%s: map buffer failed! ret = %d\n",
  1116. __func__, result);
  1117. goto done;
  1118. }
  1119. }
  1120. if (copy_from_user(&user_afe_buf, (void *)buf,
  1121. sizeof(struct rtac_afe_user_data))) {
  1122. pr_err("%s: Copy from user failed! buf = 0x%pK\n",
  1123. __func__, buf);
  1124. goto done;
  1125. }
  1126. if (user_afe_buf.buf_size <= 0) {
  1127. pr_err("%s: Invalid buffer size = %d\n",
  1128. __func__, user_afe_buf.buf_size);
  1129. goto done;
  1130. }
  1131. port_index = q6audio_get_port_index(user_afe_buf.port_id);
  1132. if (port_index >= AFE_MAX_PORTS) {
  1133. pr_err("%s: Invalid AFE port = 0x%x\n",
  1134. __func__, user_afe_buf.port_id);
  1135. goto done;
  1136. }
  1137. mutex_lock(&rtac_afe_apr_mutex);
  1138. if (rtac_afe_apr_data.apr_handle == NULL) {
  1139. pr_err("%s: APR not initialized\n", __func__);
  1140. result = -EINVAL;
  1141. goto err;
  1142. }
  1143. afe_cmd =
  1144. (u32 *) rtac_afe_buffer + sizeof(struct apr_hdr) / sizeof(u32);
  1145. switch (opcode) {
  1146. case AFE_PORT_CMD_SET_PARAM_V2:
  1147. apr_msg_size = sizeof(struct afe_port_cmd_set_param_v2);
  1148. payload_size = user_afe_buf.v2_set.payload_size;
  1149. if (payload_size > rtac_cal[AFE_RTAC_CAL].map_data.map_size) {
  1150. pr_err("%s: Invalid payload size = %d\n", __func__,
  1151. payload_size);
  1152. result = -EINVAL;
  1153. goto err;
  1154. }
  1155. /* Copy the command to the rtac buffer */
  1156. memcpy(afe_cmd, &user_afe_buf.v2_set,
  1157. sizeof(user_afe_buf.v2_set));
  1158. /* Copy the param data to the out-of-band location */
  1159. if (copy_from_user(rtac_cal[AFE_RTAC_CAL].cal_data.kvaddr,
  1160. (void __user *) buf +
  1161. offsetof(struct rtac_afe_user_data,
  1162. v2_set.param_hdr),
  1163. payload_size)) {
  1164. pr_err("%s: Could not copy payload from user buffer\n",
  1165. __func__);
  1166. result = -EFAULT;
  1167. goto err;
  1168. }
  1169. break;
  1170. case AFE_PORT_CMD_SET_PARAM_V3:
  1171. apr_msg_size = sizeof(struct afe_port_cmd_set_param_v3);
  1172. payload_size = user_afe_buf.v3_set.payload_size;
  1173. if (payload_size > rtac_cal[AFE_RTAC_CAL].map_data.map_size) {
  1174. pr_err("%s: Invalid payload size = %d\n", __func__,
  1175. payload_size);
  1176. result = -EINVAL;
  1177. goto err;
  1178. }
  1179. /* Copy the command to the rtac buffer */
  1180. memcpy(afe_cmd, &user_afe_buf.v3_set,
  1181. sizeof(user_afe_buf.v3_set));
  1182. /* Copy the param data to the out-of-band location */
  1183. if (copy_from_user(rtac_cal[AFE_RTAC_CAL].cal_data.kvaddr,
  1184. (void __user *) buf +
  1185. offsetof(struct rtac_afe_user_data,
  1186. v3_set.param_hdr),
  1187. payload_size)) {
  1188. pr_err("%s: Could not copy payload from user buffer\n",
  1189. __func__);
  1190. result = -EFAULT;
  1191. goto err;
  1192. }
  1193. break;
  1194. case AFE_PORT_CMD_GET_PARAM_V2:
  1195. apr_msg_size = sizeof(struct afe_port_cmd_get_param_v2);
  1196. if (user_afe_buf.cmd_size > MAX_PAYLOAD_SIZE) {
  1197. pr_err("%s: Invalid payload size = %d\n", __func__,
  1198. user_afe_buf.cmd_size);
  1199. result = -EINVAL;
  1200. goto err;
  1201. }
  1202. /* Copy the command and param data in-band */
  1203. if (copy_from_user(afe_cmd,
  1204. (void __user *) buf +
  1205. offsetof(struct rtac_afe_user_data,
  1206. v2_get),
  1207. user_afe_buf.cmd_size)) {
  1208. pr_err("%s: Could not copy payload from user buffer\n",
  1209. __func__);
  1210. result = -EFAULT;
  1211. goto err;
  1212. }
  1213. break;
  1214. case AFE_PORT_CMD_GET_PARAM_V3:
  1215. apr_msg_size = sizeof(struct afe_port_cmd_get_param_v3);
  1216. if (user_afe_buf.cmd_size > MAX_PAYLOAD_SIZE) {
  1217. pr_err("%s: Invalid payload size = %d\n", __func__,
  1218. user_afe_buf.cmd_size);
  1219. result = -EINVAL;
  1220. goto err;
  1221. }
  1222. /* Copy the command and param data in-band */
  1223. if (copy_from_user(afe_cmd,
  1224. (void __user *) buf +
  1225. offsetof(struct rtac_afe_user_data,
  1226. v3_get),
  1227. user_afe_buf.cmd_size)) {
  1228. pr_err("%s: Could not copy payload from user buffer\n",
  1229. __func__);
  1230. result = -EFAULT;
  1231. goto err;
  1232. }
  1233. break;
  1234. default:
  1235. pr_err("%s: Invalid opcode %d\n", __func__, opcode);
  1236. result = -EINVAL;
  1237. goto err;
  1238. }
  1239. /*
  1240. * The memory header is in the same location in all commands. Therefore,
  1241. * it doesn't matter what command the buffer is cast into.
  1242. */
  1243. mem_hdr = &((struct afe_port_cmd_set_param_v3 *) rtac_afe_buffer)
  1244. ->mem_hdr;
  1245. mem_hdr->data_payload_addr_lsw =
  1246. lower_32_bits(rtac_cal[AFE_RTAC_CAL].cal_data.paddr);
  1247. mem_hdr->data_payload_addr_msw = msm_audio_populate_upper_32_bits(
  1248. rtac_cal[AFE_RTAC_CAL].cal_data.paddr);
  1249. mem_hdr->mem_map_handle = rtac_cal[AFE_RTAC_CAL].map_data.map_handle;
  1250. /* Fill the APR header at the end so we have the correct message size */
  1251. fill_afe_apr_hdr((struct apr_hdr *) rtac_afe_buffer,
  1252. port_index, opcode, apr_msg_size);
  1253. atomic_set(&rtac_afe_apr_data.cmd_state, 1);
  1254. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
  1255. __func__, opcode,
  1256. &rtac_cal[AFE_RTAC_CAL].cal_data.paddr);
  1257. result = apr_send_pkt(rtac_afe_apr_data.apr_handle,
  1258. (uint32_t *)rtac_afe_buffer);
  1259. if (result < 0) {
  1260. pr_err("%s: Set params failed port = 0x%x, ret = %d\n",
  1261. __func__, user_afe_buf.port_id, result);
  1262. goto err;
  1263. }
  1264. /* Wait for the callback */
  1265. result = wait_event_timeout(rtac_afe_apr_data.cmd_wait,
  1266. (atomic_read(&rtac_afe_apr_data.cmd_state) == 0),
  1267. msecs_to_jiffies(TIMEOUT_MS));
  1268. if (!result) {
  1269. pr_err("%s: Set params timed out port = 0x%x, ret = %d\n",
  1270. __func__, user_afe_buf.port_id, result);
  1271. goto err;
  1272. }
  1273. if (atomic_read(&rtac_common.apr_err_code)) {
  1274. pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
  1275. __func__, adsp_err_get_err_str(atomic_read(
  1276. &rtac_common.apr_err_code)),
  1277. opcode);
  1278. result = adsp_err_get_lnx_err_code(
  1279. atomic_read(
  1280. &rtac_common.apr_err_code));
  1281. goto err;
  1282. }
  1283. if (opcode == AFE_PORT_CMD_GET_PARAM_V2) {
  1284. get_resp_v2 = (struct param_hdr_v1 *) rtac_cal[AFE_RTAC_CAL]
  1285. .cal_data.kvaddr;
  1286. bytes_returned =
  1287. get_resp_v2->param_size + sizeof(struct param_hdr_v1);
  1288. } else if (opcode == AFE_PORT_CMD_GET_PARAM_V3) {
  1289. get_resp_v3 = (struct param_hdr_v3 *) rtac_cal[AFE_RTAC_CAL]
  1290. .cal_data.kvaddr;
  1291. bytes_returned =
  1292. get_resp_v3->param_size + sizeof(struct param_hdr_v3);
  1293. } else {
  1294. bytes_returned = payload_size;
  1295. goto unlock;
  1296. }
  1297. if (bytes_returned > rtac_cal[AFE_RTAC_CAL].map_data.map_size) {
  1298. pr_err("%s: Invalid data size = %d\n", __func__,
  1299. bytes_returned);
  1300. result = -EINVAL;
  1301. goto err;
  1302. }
  1303. if (bytes_returned > user_afe_buf.buf_size) {
  1304. pr_err("%s: user size = 0x%x, returned size = 0x%x\n", __func__,
  1305. user_afe_buf.buf_size, bytes_returned);
  1306. result = -EINVAL;
  1307. goto err;
  1308. }
  1309. if (copy_to_user((void __user *) buf,
  1310. rtac_cal[AFE_RTAC_CAL].cal_data.kvaddr,
  1311. bytes_returned)) {
  1312. pr_err("%s: Could not copy buffer to user,size = %d\n",
  1313. __func__, bytes_returned);
  1314. result = -EFAULT;
  1315. goto err;
  1316. }
  1317. unlock:
  1318. mutex_unlock(&rtac_afe_apr_mutex);
  1319. done:
  1320. return bytes_returned;
  1321. err:
  1322. mutex_unlock(&rtac_afe_apr_mutex);
  1323. return result;
  1324. }
  1325. /* Voice APR */
  1326. void rtac_set_voice_handle(u32 mode, void *handle)
  1327. {
  1328. pr_debug("%s\n", __func__);
  1329. mutex_lock(&rtac_voice_apr_mutex);
  1330. rtac_voice_apr_data[mode].apr_handle = handle;
  1331. mutex_unlock(&rtac_voice_apr_mutex);
  1332. }
  1333. bool rtac_make_voice_callback(u32 mode, uint32_t *payload, u32 payload_size)
  1334. {
  1335. if ((atomic_read(&rtac_voice_apr_data[mode].cmd_state) != 1) ||
  1336. (mode >= RTAC_VOICE_MODES))
  1337. return false;
  1338. pr_debug("%s\n", __func__);
  1339. if (payload_size == sizeof(uint32_t))
  1340. atomic_set(&rtac_common.apr_err_code, payload[0]);
  1341. else if (payload_size == (2*sizeof(uint32_t)))
  1342. atomic_set(&rtac_common.apr_err_code, payload[1]);
  1343. atomic_set(&rtac_voice_apr_data[mode].cmd_state, 0);
  1344. wake_up(&rtac_voice_apr_data[mode].cmd_wait);
  1345. return true;
  1346. }
  1347. int send_voice_apr(u32 mode, void *buf, u32 opcode)
  1348. {
  1349. s32 result;
  1350. u32 user_buf_size = 0;
  1351. u32 bytes_returned = 0;
  1352. u32 payload_size;
  1353. u32 dest_port;
  1354. u32 data_size = 0;
  1355. struct apr_hdr voice_params;
  1356. pr_debug("%s\n", __func__);
  1357. if (rtac_cal[VOICE_RTAC_CAL].map_data.dma_buf == NULL) {
  1358. result = rtac_allocate_cal_buffer(VOICE_RTAC_CAL);
  1359. if (result < 0) {
  1360. pr_err("%s: allocate buffer failed!",
  1361. __func__);
  1362. goto done;
  1363. }
  1364. }
  1365. if (rtac_cal[VOICE_RTAC_CAL].map_data.map_handle == 0) {
  1366. result = rtac_map_cal_buffer(VOICE_RTAC_CAL);
  1367. if (result < 0) {
  1368. pr_err("%s: map buffer failed!",
  1369. __func__);
  1370. goto done;
  1371. }
  1372. }
  1373. if (copy_from_user(&user_buf_size, (void *)buf,
  1374. sizeof(user_buf_size))) {
  1375. pr_err("%s: Copy from user failed! buf = 0x%pK\n",
  1376. __func__, buf);
  1377. goto done;
  1378. }
  1379. if (user_buf_size <= 0) {
  1380. pr_err("%s: Invalid buffer size = %d\n",
  1381. __func__, user_buf_size);
  1382. goto done;
  1383. }
  1384. if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
  1385. pr_err("%s: Could not copy payload size from user buffer\n",
  1386. __func__);
  1387. goto done;
  1388. }
  1389. if (copy_from_user(&dest_port, buf + 2 * sizeof(u32), sizeof(u32))) {
  1390. pr_err("%s: Could not copy port id from user buffer\n",
  1391. __func__);
  1392. goto done;
  1393. }
  1394. if ((mode != RTAC_CVP) && (mode != RTAC_CVS)) {
  1395. pr_err("%s: Invalid Mode for APR, mode = %d\n",
  1396. __func__, mode);
  1397. goto done;
  1398. }
  1399. mutex_lock(&rtac_voice_apr_mutex);
  1400. if (rtac_voice_apr_data[mode].apr_handle == NULL) {
  1401. pr_err("%s: APR not initialized\n", __func__);
  1402. result = -EINVAL;
  1403. goto err;
  1404. }
  1405. switch (opcode) {
  1406. case VSS_ICOMMON_CMD_SET_PARAM_V2:
  1407. case VSS_ICOMMON_CMD_SET_PARAM_V3:
  1408. /* set payload size to in-band payload */
  1409. /* set data size to actual out of band payload size */
  1410. data_size = payload_size - 4 * sizeof(u32);
  1411. if (data_size > rtac_cal[VOICE_RTAC_CAL].map_data.map_size) {
  1412. pr_err("%s: Invalid data size = %d\n",
  1413. __func__, data_size);
  1414. result = -EINVAL;
  1415. goto err;
  1416. }
  1417. payload_size = 4 * sizeof(u32);
  1418. /* Copy buffer to out-of-band payload */
  1419. if (copy_from_user((void *)
  1420. rtac_cal[VOICE_RTAC_CAL].cal_data.kvaddr,
  1421. buf + 7 * sizeof(u32), data_size)) {
  1422. pr_err("%s: Could not copy payload from user buffer\n",
  1423. __func__);
  1424. result = -EFAULT;
  1425. goto err;
  1426. }
  1427. /* set payload size in packet */
  1428. rtac_voice_buffer[8] = data_size;
  1429. /* set token for set param case */
  1430. voice_params.token = VOC_RTAC_SET_PARAM_TOKEN;
  1431. break;
  1432. case VSS_ICOMMON_CMD_GET_PARAM_V2:
  1433. case VSS_ICOMMON_CMD_GET_PARAM_V3:
  1434. if (payload_size > MAX_PAYLOAD_SIZE) {
  1435. pr_err("%s: Invalid payload size = %d\n",
  1436. __func__, payload_size);
  1437. result = -EINVAL;
  1438. goto err;
  1439. }
  1440. /* Copy buffer to in-band payload */
  1441. if (copy_from_user(rtac_voice_buffer +
  1442. sizeof(voice_params)/sizeof(u32),
  1443. buf + 3 * sizeof(u32), payload_size)) {
  1444. pr_err("%s: Could not copy payload from user buffer\n",
  1445. __func__);
  1446. result = -EFAULT;
  1447. goto err;
  1448. }
  1449. /* set token for get param case */
  1450. voice_params.token = 0;
  1451. break;
  1452. default:
  1453. pr_err("%s: Invalid opcode %d\n", __func__, opcode);
  1454. result = -EINVAL;
  1455. goto err;
  1456. }
  1457. /* Pack header */
  1458. voice_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1459. APR_HDR_LEN(20), APR_PKT_VER);
  1460. voice_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1461. payload_size);
  1462. voice_params.src_svc = 0;
  1463. voice_params.src_domain = APR_DOMAIN_APPS;
  1464. voice_params.src_port = get_voice_index(mode, dest_port);
  1465. voice_params.dest_svc = 0;
  1466. voice_params.dest_domain = APR_DOMAIN_MODEM;
  1467. voice_params.dest_port = (u16)dest_port;
  1468. voice_params.opcode = opcode;
  1469. /* fill for out-of-band */
  1470. rtac_voice_buffer[5] = rtac_cal[VOICE_RTAC_CAL].map_data.map_handle;
  1471. rtac_voice_buffer[6] =
  1472. lower_32_bits(rtac_cal[VOICE_RTAC_CAL].cal_data.paddr);
  1473. rtac_voice_buffer[7] = msm_audio_populate_upper_32_bits(
  1474. rtac_cal[VOICE_RTAC_CAL].cal_data.paddr);
  1475. memcpy(rtac_voice_buffer, &voice_params, sizeof(voice_params));
  1476. atomic_set(&rtac_voice_apr_data[mode].cmd_state, 1);
  1477. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
  1478. __func__, opcode,
  1479. &rtac_cal[VOICE_RTAC_CAL].cal_data.paddr);
  1480. result = apr_send_pkt(rtac_voice_apr_data[mode].apr_handle,
  1481. (uint32_t *)rtac_voice_buffer);
  1482. if (result < 0) {
  1483. pr_err("%s: apr_send_pkt failed opcode = %x\n",
  1484. __func__, opcode);
  1485. goto err;
  1486. }
  1487. /* Wait for the callback */
  1488. result = wait_event_timeout(rtac_voice_apr_data[mode].cmd_wait,
  1489. (atomic_read(&rtac_voice_apr_data[mode].cmd_state) == 0),
  1490. msecs_to_jiffies(TIMEOUT_MS));
  1491. if (!result) {
  1492. pr_err("%s: apr_send_pkt timed out opcode = %x\n",
  1493. __func__, opcode);
  1494. goto err;
  1495. }
  1496. if (atomic_read(&rtac_common.apr_err_code)) {
  1497. pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
  1498. __func__, adsp_err_get_err_str(atomic_read(
  1499. &rtac_common.apr_err_code)),
  1500. opcode);
  1501. result = adsp_err_get_lnx_err_code(
  1502. atomic_read(
  1503. &rtac_common.apr_err_code));
  1504. goto err;
  1505. }
  1506. if (opcode == VSS_ICOMMON_CMD_GET_PARAM_V2) {
  1507. bytes_returned = ((u32 *)rtac_cal[VOICE_RTAC_CAL].cal_data.
  1508. kvaddr)[2] + 3 * sizeof(u32);
  1509. } else if (opcode == VSS_ICOMMON_CMD_GET_PARAM_V3) {
  1510. bytes_returned =
  1511. ((u32 *) rtac_cal[VOICE_RTAC_CAL].cal_data.kvaddr)[3] +
  1512. 4 * sizeof(u32);
  1513. } else {
  1514. bytes_returned = data_size;
  1515. goto unlock;
  1516. }
  1517. if (bytes_returned > rtac_cal[VOICE_RTAC_CAL].map_data.map_size) {
  1518. pr_err("%s: Invalid data size = %d\n", __func__,
  1519. bytes_returned);
  1520. result = -EINVAL;
  1521. goto err;
  1522. }
  1523. if (bytes_returned > user_buf_size) {
  1524. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  1525. __func__, user_buf_size, bytes_returned);
  1526. result = -EINVAL;
  1527. goto err;
  1528. }
  1529. if (copy_to_user((void __user *) buf,
  1530. rtac_cal[VOICE_RTAC_CAL].cal_data.kvaddr,
  1531. bytes_returned)) {
  1532. pr_err("%s: Could not copy buffer to user, size = %d\n",
  1533. __func__, bytes_returned);
  1534. result = -EFAULT;
  1535. goto err;
  1536. }
  1537. unlock:
  1538. mutex_unlock(&rtac_voice_apr_mutex);
  1539. done:
  1540. return bytes_returned;
  1541. err:
  1542. mutex_unlock(&rtac_voice_apr_mutex);
  1543. return result;
  1544. }
  1545. void get_rtac_adm_data(struct rtac_adm *adm_data)
  1546. {
  1547. mutex_lock(&rtac_adm_mutex);
  1548. memcpy(adm_data, &rtac_adm_data, sizeof(struct rtac_adm));
  1549. mutex_unlock(&rtac_adm_mutex);
  1550. }
  1551. static long rtac_ioctl_shared(struct file *f,
  1552. unsigned int cmd, void *arg)
  1553. {
  1554. u32 opcode;
  1555. int result = 0;
  1556. if (!arg) {
  1557. pr_err("%s: No data sent to driver!\n", __func__);
  1558. result = -EFAULT;
  1559. goto done;
  1560. }
  1561. switch (cmd) {
  1562. case AUDIO_GET_RTAC_ADM_INFO: {
  1563. mutex_lock(&rtac_adm_mutex);
  1564. if (copy_to_user((void *)arg, &rtac_adm_data,
  1565. sizeof(rtac_adm_data))) {
  1566. pr_err("%s: copy_to_user failed for AUDIO_GET_RTAC_ADM_INFO\n",
  1567. __func__);
  1568. mutex_unlock(&rtac_adm_mutex);
  1569. return -EFAULT;
  1570. }
  1571. result = sizeof(rtac_adm_data);
  1572. mutex_unlock(&rtac_adm_mutex);
  1573. break;
  1574. }
  1575. case AUDIO_GET_RTAC_VOICE_INFO: {
  1576. mutex_lock(&rtac_voice_mutex);
  1577. if (copy_to_user((void *)arg, &rtac_voice_data,
  1578. sizeof(rtac_voice_data))) {
  1579. pr_err("%s: copy_to_user failed for AUDIO_GET_RTAC_VOICE_INFO\n",
  1580. __func__);
  1581. mutex_unlock(&rtac_voice_mutex);
  1582. return -EFAULT;
  1583. }
  1584. result = sizeof(rtac_voice_data);
  1585. mutex_unlock(&rtac_voice_mutex);
  1586. break;
  1587. }
  1588. case AUDIO_GET_RTAC_ADM_CAL:
  1589. opcode = q6common_is_instance_id_supported() ?
  1590. ADM_CMD_GET_PP_PARAMS_V6 :
  1591. ADM_CMD_GET_PP_PARAMS_V5;
  1592. result = send_adm_apr((void *) arg, opcode);
  1593. break;
  1594. case AUDIO_SET_RTAC_ADM_CAL:
  1595. opcode = q6common_is_instance_id_supported() ?
  1596. ADM_CMD_SET_PP_PARAMS_V6 :
  1597. ADM_CMD_SET_PP_PARAMS_V5;
  1598. result = send_adm_apr((void *) arg, opcode);
  1599. break;
  1600. case AUDIO_GET_RTAC_ASM_CAL:
  1601. opcode = q6common_is_instance_id_supported() ?
  1602. ASM_STREAM_CMD_GET_PP_PARAMS_V3 :
  1603. ASM_STREAM_CMD_GET_PP_PARAMS_V2;
  1604. result = send_rtac_asm_apr((void *) arg, opcode);
  1605. break;
  1606. case AUDIO_SET_RTAC_ASM_CAL:
  1607. opcode = q6common_is_instance_id_supported() ?
  1608. ASM_STREAM_CMD_SET_PP_PARAMS_V3 :
  1609. ASM_STREAM_CMD_SET_PP_PARAMS_V2;
  1610. result = send_rtac_asm_apr((void *) arg, opcode);
  1611. break;
  1612. case AUDIO_GET_RTAC_CVS_CAL:
  1613. opcode = q6common_is_instance_id_supported() ?
  1614. VSS_ICOMMON_CMD_GET_PARAM_V3 :
  1615. VSS_ICOMMON_CMD_GET_PARAM_V2;
  1616. result = send_voice_apr(RTAC_CVS, (void *) arg, opcode);
  1617. break;
  1618. case AUDIO_SET_RTAC_CVS_CAL:
  1619. opcode = q6common_is_instance_id_supported() ?
  1620. VSS_ICOMMON_CMD_SET_PARAM_V3 :
  1621. VSS_ICOMMON_CMD_SET_PARAM_V2;
  1622. result = send_voice_apr(RTAC_CVS, (void *) arg, opcode);
  1623. break;
  1624. case AUDIO_GET_RTAC_CVP_CAL:
  1625. opcode = q6common_is_instance_id_supported() ?
  1626. VSS_ICOMMON_CMD_GET_PARAM_V3 :
  1627. VSS_ICOMMON_CMD_GET_PARAM_V2;
  1628. result = send_voice_apr(RTAC_CVP, (void *) arg, opcode);
  1629. break;
  1630. case AUDIO_SET_RTAC_CVP_CAL:
  1631. opcode = q6common_is_instance_id_supported() ?
  1632. VSS_ICOMMON_CMD_SET_PARAM_V3 :
  1633. VSS_ICOMMON_CMD_SET_PARAM_V2;
  1634. result = send_voice_apr(RTAC_CVP, (void *) arg, opcode);
  1635. break;
  1636. case AUDIO_GET_RTAC_AFE_CAL:
  1637. opcode = q6common_is_instance_id_supported() ?
  1638. AFE_PORT_CMD_GET_PARAM_V3 :
  1639. AFE_PORT_CMD_GET_PARAM_V2;
  1640. result = send_rtac_afe_apr((void __user *) arg, opcode);
  1641. break;
  1642. case AUDIO_SET_RTAC_AFE_CAL:
  1643. opcode = q6common_is_instance_id_supported() ?
  1644. AFE_PORT_CMD_SET_PARAM_V3 :
  1645. AFE_PORT_CMD_SET_PARAM_V2;
  1646. result = send_rtac_afe_apr((void __user *) arg, opcode);
  1647. break;
  1648. default:
  1649. pr_err("%s: Invalid IOCTL, command = %d!\n",
  1650. __func__, cmd);
  1651. result = -EINVAL;
  1652. }
  1653. done:
  1654. return result;
  1655. }
  1656. static long rtac_ioctl(struct file *f,
  1657. unsigned int cmd, unsigned long arg)
  1658. {
  1659. int result = 0;
  1660. mutex_lock(&rtac_common.rtac_fops_mutex);
  1661. if (!arg) {
  1662. pr_err("%s: No data sent to driver!\n", __func__);
  1663. result = -EFAULT;
  1664. } else {
  1665. result = rtac_ioctl_shared(f, cmd, (void __user *)arg);
  1666. }
  1667. mutex_unlock(&rtac_common.rtac_fops_mutex);
  1668. return result;
  1669. }
  1670. #ifdef CONFIG_COMPAT
  1671. #define AUDIO_GET_RTAC_ADM_INFO_32 _IOR(CAL_IOCTL_MAGIC, 207, compat_uptr_t)
  1672. #define AUDIO_GET_RTAC_VOICE_INFO_32 _IOR(CAL_IOCTL_MAGIC, 208, compat_uptr_t)
  1673. #define AUDIO_GET_RTAC_ADM_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 209, compat_uptr_t)
  1674. #define AUDIO_SET_RTAC_ADM_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 210, compat_uptr_t)
  1675. #define AUDIO_GET_RTAC_ASM_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 211, compat_uptr_t)
  1676. #define AUDIO_SET_RTAC_ASM_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 212, compat_uptr_t)
  1677. #define AUDIO_GET_RTAC_CVS_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 213, compat_uptr_t)
  1678. #define AUDIO_SET_RTAC_CVS_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 214, compat_uptr_t)
  1679. #define AUDIO_GET_RTAC_CVP_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 215, compat_uptr_t)
  1680. #define AUDIO_SET_RTAC_CVP_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 216, compat_uptr_t)
  1681. #define AUDIO_GET_RTAC_AFE_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 217, compat_uptr_t)
  1682. #define AUDIO_SET_RTAC_AFE_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 218, compat_uptr_t)
  1683. static long rtac_compat_ioctl(struct file *f,
  1684. unsigned int cmd, unsigned long arg)
  1685. {
  1686. int result = 0;
  1687. mutex_lock(&rtac_common.rtac_fops_mutex);
  1688. if (!arg) {
  1689. pr_err("%s: No data sent to driver!\n", __func__);
  1690. result = -EINVAL;
  1691. goto done;
  1692. }
  1693. switch (cmd) {
  1694. case AUDIO_GET_RTAC_ADM_INFO_32:
  1695. cmd = AUDIO_GET_RTAC_ADM_INFO;
  1696. goto process;
  1697. case AUDIO_GET_RTAC_VOICE_INFO_32:
  1698. cmd = AUDIO_GET_RTAC_VOICE_INFO;
  1699. goto process;
  1700. case AUDIO_GET_RTAC_AFE_CAL_32:
  1701. cmd = AUDIO_GET_RTAC_AFE_CAL;
  1702. goto process;
  1703. case AUDIO_SET_RTAC_AFE_CAL_32:
  1704. cmd = AUDIO_SET_RTAC_AFE_CAL;
  1705. goto process;
  1706. case AUDIO_GET_RTAC_ADM_CAL_32:
  1707. cmd = AUDIO_GET_RTAC_ADM_CAL;
  1708. goto process;
  1709. case AUDIO_SET_RTAC_ADM_CAL_32:
  1710. cmd = AUDIO_SET_RTAC_ADM_CAL;
  1711. goto process;
  1712. case AUDIO_GET_RTAC_ASM_CAL_32:
  1713. cmd = AUDIO_GET_RTAC_ASM_CAL;
  1714. goto process;
  1715. case AUDIO_SET_RTAC_ASM_CAL_32:
  1716. cmd = AUDIO_SET_RTAC_ASM_CAL;
  1717. goto process;
  1718. case AUDIO_GET_RTAC_CVS_CAL_32:
  1719. cmd = AUDIO_GET_RTAC_CVS_CAL;
  1720. goto process;
  1721. case AUDIO_SET_RTAC_CVS_CAL_32:
  1722. cmd = AUDIO_SET_RTAC_CVS_CAL;
  1723. goto process;
  1724. case AUDIO_GET_RTAC_CVP_CAL_32:
  1725. cmd = AUDIO_GET_RTAC_CVP_CAL;
  1726. goto process;
  1727. case AUDIO_SET_RTAC_CVP_CAL_32:
  1728. cmd = AUDIO_SET_RTAC_CVP_CAL;
  1729. process:
  1730. result = rtac_ioctl_shared(f, cmd, compat_ptr(arg));
  1731. break;
  1732. default:
  1733. result = -EINVAL;
  1734. pr_err("%s: Invalid IOCTL, command = %d!\n",
  1735. __func__, cmd);
  1736. break;
  1737. }
  1738. done:
  1739. mutex_unlock(&rtac_common.rtac_fops_mutex);
  1740. return result;
  1741. }
  1742. #else
  1743. #define rtac_compat_ioctl NULL
  1744. #endif
  1745. static const struct file_operations rtac_fops = {
  1746. .owner = THIS_MODULE,
  1747. .open = rtac_open,
  1748. .release = rtac_release,
  1749. .unlocked_ioctl = rtac_ioctl,
  1750. .compat_ioctl = rtac_compat_ioctl,
  1751. };
  1752. struct miscdevice rtac_misc = {
  1753. .minor = MISC_DYNAMIC_MINOR,
  1754. .name = "msm_rtac",
  1755. .fops = &rtac_fops,
  1756. };
  1757. int __init rtac_init(void)
  1758. {
  1759. int i = 0;
  1760. /* Driver */
  1761. atomic_set(&rtac_common.usage_count, 0);
  1762. atomic_set(&rtac_common.apr_err_code, 0);
  1763. mutex_init(&rtac_common.rtac_fops_mutex);
  1764. /* ADM */
  1765. memset(&rtac_adm_data, 0, sizeof(rtac_adm_data));
  1766. rtac_adm_apr_data.apr_handle = NULL;
  1767. atomic_set(&rtac_adm_apr_data.cmd_state, 0);
  1768. init_waitqueue_head(&rtac_adm_apr_data.cmd_wait);
  1769. mutex_init(&rtac_adm_mutex);
  1770. mutex_init(&rtac_adm_apr_mutex);
  1771. rtac_adm_buffer = kzalloc(
  1772. rtac_cal[ADM_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1773. if (rtac_adm_buffer == NULL)
  1774. goto nomem;
  1775. /* ASM */
  1776. for (i = 0; i < ASM_ACTIVE_STREAMS_ALLOWED+1; i++) {
  1777. rtac_asm_apr_data[i].apr_handle = NULL;
  1778. atomic_set(&rtac_asm_apr_data[i].cmd_state, 0);
  1779. init_waitqueue_head(&rtac_asm_apr_data[i].cmd_wait);
  1780. }
  1781. mutex_init(&rtac_asm_apr_mutex);
  1782. rtac_asm_buffer = kzalloc(
  1783. rtac_cal[ASM_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1784. if (rtac_asm_buffer == NULL) {
  1785. kzfree(rtac_adm_buffer);
  1786. goto nomem;
  1787. }
  1788. /* AFE */
  1789. rtac_afe_apr_data.apr_handle = NULL;
  1790. atomic_set(&rtac_afe_apr_data.cmd_state, 0);
  1791. init_waitqueue_head(&rtac_afe_apr_data.cmd_wait);
  1792. mutex_init(&rtac_afe_apr_mutex);
  1793. rtac_afe_buffer = kzalloc(
  1794. rtac_cal[AFE_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1795. if (rtac_afe_buffer == NULL) {
  1796. kzfree(rtac_adm_buffer);
  1797. kzfree(rtac_asm_buffer);
  1798. goto nomem;
  1799. }
  1800. /* Voice */
  1801. memset(&rtac_voice_data, 0, sizeof(rtac_voice_data));
  1802. for (i = 0; i < RTAC_VOICE_MODES; i++) {
  1803. rtac_voice_apr_data[i].apr_handle = NULL;
  1804. atomic_set(&rtac_voice_apr_data[i].cmd_state, 0);
  1805. init_waitqueue_head(&rtac_voice_apr_data[i].cmd_wait);
  1806. }
  1807. mutex_init(&rtac_voice_mutex);
  1808. mutex_init(&rtac_voice_apr_mutex);
  1809. rtac_voice_buffer = kzalloc(
  1810. rtac_cal[VOICE_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1811. if (rtac_voice_buffer == NULL) {
  1812. kzfree(rtac_adm_buffer);
  1813. kzfree(rtac_asm_buffer);
  1814. kzfree(rtac_afe_buffer);
  1815. goto nomem;
  1816. }
  1817. if (misc_register(&rtac_misc) != 0) {
  1818. kzfree(rtac_adm_buffer);
  1819. kzfree(rtac_asm_buffer);
  1820. kzfree(rtac_afe_buffer);
  1821. kzfree(rtac_voice_buffer);
  1822. goto nomem;
  1823. }
  1824. return 0;
  1825. nomem:
  1826. return -ENOMEM;
  1827. }
  1828. void rtac_exit(void)
  1829. {
  1830. misc_deregister(&rtac_misc);
  1831. kzfree(rtac_adm_buffer);
  1832. kzfree(rtac_asm_buffer);
  1833. kzfree(rtac_afe_buffer);
  1834. kzfree(rtac_voice_buffer);
  1835. }
  1836. MODULE_DESCRIPTION("SoC QDSP6v2 Real-Time Audio Calibration driver");
  1837. MODULE_LICENSE("GPL v2");