rtac.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961
  1. /* Copyright (c) 2012-2017, 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/q6voice.h>
  30. #include "adsp_err.h"
  31. /* Max size of payload (buf size - apr header) */
  32. #define MAX_PAYLOAD_SIZE 4076
  33. #define RTAC_MAX_ACTIVE_VOICE_COMBOS 2
  34. #define RTAC_MAX_ACTIVE_POPP 8
  35. #define RTAC_BUF_SIZE 163840
  36. #define TIMEOUT_MS 1000
  37. struct rtac_cal_block_data rtac_cal[MAX_RTAC_BLOCKS] = {
  38. /* ADM_RTAC_CAL */
  39. {{RTAC_BUF_SIZE, 0, 0, 0}, {0, 0, 0} },
  40. /* ASM_RTAC_CAL */
  41. {{RTAC_BUF_SIZE, 0, 0, 0}, {0, 0, 0} },
  42. /* VOICE_RTAC_CAL */
  43. {{RTAC_BUF_SIZE, 0, 0, 0}, {0, 0, 0} },
  44. /* AFE_RTAC_CAL */
  45. {{RTAC_BUF_SIZE, 0, 0, 0}, {0, 0, 0} }
  46. };
  47. struct rtac_common_data {
  48. atomic_t usage_count;
  49. atomic_t apr_err_code;
  50. };
  51. static struct rtac_common_data rtac_common;
  52. /* APR data */
  53. struct rtac_apr_data {
  54. void *apr_handle;
  55. atomic_t cmd_state;
  56. wait_queue_head_t cmd_wait;
  57. };
  58. static struct rtac_apr_data rtac_adm_apr_data;
  59. static struct rtac_apr_data rtac_asm_apr_data[ASM_ACTIVE_STREAMS_ALLOWED + 1];
  60. static struct rtac_apr_data rtac_afe_apr_data;
  61. static struct rtac_apr_data rtac_voice_apr_data[RTAC_VOICE_MODES];
  62. /* ADM info & APR */
  63. static struct rtac_adm rtac_adm_data;
  64. static u32 *rtac_adm_buffer;
  65. /* ASM APR */
  66. static u32 *rtac_asm_buffer;
  67. static u32 *rtac_afe_buffer;
  68. /* Voice info & APR */
  69. struct rtac_voice_data_t {
  70. uint32_t tx_topology_id;
  71. uint32_t rx_topology_id;
  72. uint32_t tx_afe_topology;
  73. uint32_t rx_afe_topology;
  74. uint32_t tx_afe_port;
  75. uint32_t rx_afe_port;
  76. uint16_t cvs_handle;
  77. uint16_t cvp_handle;
  78. uint32_t tx_acdb_id;
  79. uint32_t rx_acdb_id;
  80. };
  81. struct rtac_voice {
  82. uint32_t num_of_voice_combos;
  83. struct rtac_voice_data_t voice[RTAC_MAX_ACTIVE_VOICE_COMBOS];
  84. };
  85. struct rtac_afe_user_data {
  86. uint32_t buf_size;
  87. uint32_t cmd_size;
  88. uint32_t port_id;
  89. union {
  90. struct rtac_afe_set {
  91. struct afe_port_cmd_set_param_v2 cmd;
  92. struct afe_port_param_data_v2 data;
  93. } rtac_afe_set;
  94. struct rtac_afe_get {
  95. struct afe_port_cmd_get_param_v2 cmd;
  96. struct afe_port_param_data_v2 data;
  97. } rtac_afe_get;
  98. };
  99. } __packed;
  100. static struct rtac_voice rtac_voice_data;
  101. static u32 *rtac_voice_buffer;
  102. static u32 voice_session_id[RTAC_MAX_ACTIVE_VOICE_COMBOS];
  103. struct mutex rtac_adm_mutex;
  104. struct mutex rtac_adm_apr_mutex;
  105. struct mutex rtac_asm_apr_mutex;
  106. struct mutex rtac_voice_mutex;
  107. struct mutex rtac_voice_apr_mutex;
  108. struct mutex rtac_afe_apr_mutex;
  109. int rtac_clear_mapping(uint32_t cal_type)
  110. {
  111. int result = 0;
  112. pr_debug("%s\n", __func__);
  113. if (cal_type >= MAX_RTAC_BLOCKS) {
  114. pr_debug("%s: invalid cal type %d\n", __func__, cal_type);
  115. result = -EINVAL;
  116. goto done;
  117. }
  118. rtac_cal[cal_type].map_data.map_handle = 0;
  119. done:
  120. return result;
  121. }
  122. int rtac_allocate_cal_buffer(uint32_t cal_type)
  123. {
  124. int result = 0;
  125. size_t len;
  126. pr_debug("%s\n", __func__);
  127. if (cal_type >= MAX_RTAC_BLOCKS) {
  128. pr_err("%s: cal_type %d is invalid!\n",
  129. __func__, cal_type);
  130. result = -EINVAL;
  131. goto done;
  132. }
  133. if (rtac_cal[cal_type].cal_data.paddr != 0) {
  134. pr_err("%s: memory already allocated! cal_type %d, paddr 0x%pK\n",
  135. __func__, cal_type, &rtac_cal[cal_type].cal_data.paddr);
  136. result = -EPERM;
  137. goto done;
  138. }
  139. result = msm_audio_ion_alloc("rtac_client",
  140. &rtac_cal[cal_type].map_data.ion_client,
  141. &rtac_cal[cal_type].map_data.ion_handle,
  142. rtac_cal[cal_type].map_data.map_size,
  143. &rtac_cal[cal_type].cal_data.paddr,
  144. &len,
  145. &rtac_cal[cal_type].cal_data.kvaddr);
  146. if (result < 0) {
  147. pr_err("%s: ION create client for RTAC failed\n",
  148. __func__);
  149. goto done;
  150. }
  151. pr_debug("%s: cal_type %d, paddr 0x%pK, kvaddr 0x%pK, map_size 0x%x\n",
  152. __func__, cal_type,
  153. &rtac_cal[cal_type].cal_data.paddr,
  154. rtac_cal[cal_type].cal_data.kvaddr,
  155. rtac_cal[cal_type].map_data.map_size);
  156. done:
  157. return result;
  158. }
  159. int rtac_free_cal_buffer(uint32_t cal_type)
  160. {
  161. int result = 0;
  162. pr_debug("%s\n", __func__);
  163. if (cal_type >= MAX_RTAC_BLOCKS) {
  164. pr_err("%s: cal_type %d is invalid!\n",
  165. __func__, cal_type);
  166. result = -EINVAL;
  167. goto done;
  168. }
  169. if (rtac_cal[cal_type].map_data.ion_client == NULL) {
  170. pr_debug("%s: cal_type %d not allocated!\n",
  171. __func__, cal_type);
  172. goto done;
  173. }
  174. result = msm_audio_ion_free(rtac_cal[cal_type].map_data.ion_client,
  175. rtac_cal[cal_type].map_data.ion_handle);
  176. if (result < 0) {
  177. pr_err("%s: ION free for RTAC failed! cal_type %d, paddr 0x%pK\n",
  178. __func__, cal_type, &rtac_cal[cal_type].cal_data.paddr);
  179. goto done;
  180. }
  181. rtac_cal[cal_type].map_data.map_handle = 0;
  182. rtac_cal[cal_type].map_data.ion_client = NULL;
  183. rtac_cal[cal_type].map_data.ion_handle = NULL;
  184. rtac_cal[cal_type].cal_data.size = 0;
  185. rtac_cal[cal_type].cal_data.kvaddr = 0;
  186. rtac_cal[cal_type].cal_data.paddr = 0;
  187. done:
  188. return result;
  189. }
  190. int rtac_map_cal_buffer(uint32_t cal_type)
  191. {
  192. int result = 0;
  193. pr_debug("%s\n", __func__);
  194. if (cal_type >= MAX_RTAC_BLOCKS) {
  195. pr_err("%s: cal_type %d is invalid!\n",
  196. __func__, cal_type);
  197. result = -EINVAL;
  198. goto done;
  199. }
  200. if (rtac_cal[cal_type].map_data.map_handle != 0) {
  201. pr_err("%s: already mapped cal_type %d\n",
  202. __func__, cal_type);
  203. result = -EPERM;
  204. goto done;
  205. }
  206. if (rtac_cal[cal_type].cal_data.paddr == 0) {
  207. pr_err("%s: physical address is NULL cal_type %d\n",
  208. __func__, cal_type);
  209. result = -EPERM;
  210. goto done;
  211. }
  212. switch (cal_type) {
  213. case ADM_RTAC_CAL:
  214. result = adm_map_rtac_block(&rtac_cal[cal_type]);
  215. break;
  216. case ASM_RTAC_CAL:
  217. result = q6asm_map_rtac_block(&rtac_cal[cal_type]);
  218. break;
  219. case VOICE_RTAC_CAL:
  220. result = voc_map_rtac_block(&rtac_cal[cal_type]);
  221. break;
  222. case AFE_RTAC_CAL:
  223. result = afe_map_rtac_block(&rtac_cal[cal_type]);
  224. break;
  225. }
  226. if (result < 0) {
  227. pr_err("%s: map RTAC failed! cal_type %d\n",
  228. __func__, cal_type);
  229. goto done;
  230. }
  231. done:
  232. return result;
  233. }
  234. int rtac_unmap_cal_buffer(uint32_t cal_type)
  235. {
  236. int result = 0;
  237. pr_debug("%s\n", __func__);
  238. if (cal_type >= MAX_RTAC_BLOCKS) {
  239. pr_err("%s: cal_type %d is invalid!\n",
  240. __func__, cal_type);
  241. result = -EINVAL;
  242. goto done;
  243. }
  244. if (rtac_cal[cal_type].map_data.map_handle == 0) {
  245. pr_debug("%s: nothing to unmap cal_type %d\n",
  246. __func__, cal_type);
  247. goto done;
  248. }
  249. switch (cal_type) {
  250. case ADM_RTAC_CAL:
  251. result = adm_unmap_rtac_block(
  252. &rtac_cal[cal_type].map_data.map_handle);
  253. break;
  254. case ASM_RTAC_CAL:
  255. result = q6asm_unmap_rtac_block(
  256. &rtac_cal[cal_type].map_data.map_handle);
  257. break;
  258. case VOICE_RTAC_CAL:
  259. result = voc_unmap_rtac_block(
  260. &rtac_cal[cal_type].map_data.map_handle);
  261. break;
  262. case AFE_RTAC_CAL:
  263. result = afe_unmap_rtac_block(
  264. &rtac_cal[cal_type].map_data.map_handle);
  265. break;
  266. }
  267. if (result < 0) {
  268. pr_err("%s: unmap RTAC failed! cal_type %d\n",
  269. __func__, cal_type);
  270. goto done;
  271. }
  272. done:
  273. return result;
  274. }
  275. static int rtac_open(struct inode *inode, struct file *f)
  276. {
  277. int result = 0;
  278. pr_debug("%s\n", __func__);
  279. atomic_inc(&rtac_common.usage_count);
  280. return result;
  281. }
  282. static int rtac_release(struct inode *inode, struct file *f)
  283. {
  284. int result = 0;
  285. int result2 = 0;
  286. int i;
  287. pr_debug("%s\n", __func__);
  288. atomic_dec(&rtac_common.usage_count);
  289. pr_debug("%s: ref count %d!\n", __func__,
  290. atomic_read(&rtac_common.usage_count));
  291. if (atomic_read(&rtac_common.usage_count) > 0)
  292. goto done;
  293. for (i = 0; i < MAX_RTAC_BLOCKS; i++) {
  294. result2 = rtac_unmap_cal_buffer(i);
  295. if (result2 < 0) {
  296. pr_err("%s: unmap buffer failed! error %d!\n",
  297. __func__, result2);
  298. result = result2;
  299. }
  300. result2 = rtac_free_cal_buffer(i);
  301. if (result2 < 0) {
  302. pr_err("%s: free buffer failed! error %d!\n",
  303. __func__, result2);
  304. result = result2;
  305. }
  306. }
  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.ion_handle == 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. if (opcode == ADM_CMD_SET_PP_PARAMS_V5) {
  697. /* set payload size to in-band payload */
  698. /* set data size to actual out of band payload size */
  699. data_size = payload_size - 4 * sizeof(u32);
  700. if (data_size > rtac_cal[ADM_RTAC_CAL].map_data.map_size) {
  701. pr_err("%s: Invalid data size = %d\n",
  702. __func__, data_size);
  703. result = -EINVAL;
  704. goto err;
  705. }
  706. payload_size = 4 * sizeof(u32);
  707. /* Copy buffer to out-of-band payload */
  708. if (copy_from_user((void *)
  709. rtac_cal[ADM_RTAC_CAL].cal_data.kvaddr,
  710. buf + 7 * sizeof(u32), data_size)) {
  711. pr_err("%s: Could not copy payload from user buffer\n",
  712. __func__);
  713. result = -EINVAL;
  714. goto err;
  715. }
  716. /* set payload size in packet */
  717. rtac_adm_buffer[8] = data_size;
  718. } else {
  719. if (payload_size > MAX_PAYLOAD_SIZE) {
  720. pr_err("%s: Invalid payload size = %d\n",
  721. __func__, payload_size);
  722. result = -EINVAL;
  723. goto err;
  724. }
  725. /* Copy buffer to in-band payload */
  726. if (copy_from_user(rtac_adm_buffer +
  727. sizeof(adm_params)/sizeof(u32),
  728. buf + 3 * sizeof(u32), payload_size)) {
  729. pr_err("%s: Could not copy payload from user buffer\n",
  730. __func__);
  731. result = -EINVAL;
  732. goto err;
  733. }
  734. }
  735. /* Pack header */
  736. adm_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  737. APR_HDR_LEN(20), APR_PKT_VER);
  738. adm_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  739. payload_size);
  740. adm_params.src_svc = APR_SVC_ADM;
  741. adm_params.src_domain = APR_DOMAIN_APPS;
  742. adm_params.src_port = copp_id;
  743. adm_params.dest_svc = APR_SVC_ADM;
  744. adm_params.dest_domain = APR_DOMAIN_ADSP;
  745. adm_params.dest_port = copp_id;
  746. adm_params.token = port_idx << 16 | copp_idx;
  747. adm_params.opcode = opcode;
  748. /* fill for out-of-band */
  749. rtac_adm_buffer[5] =
  750. lower_32_bits(rtac_cal[ADM_RTAC_CAL].cal_data.paddr);
  751. rtac_adm_buffer[6] =
  752. msm_audio_populate_upper_32_bits(
  753. rtac_cal[ADM_RTAC_CAL].cal_data.paddr);
  754. rtac_adm_buffer[7] = rtac_cal[ADM_RTAC_CAL].map_data.map_handle;
  755. memcpy(rtac_adm_buffer, &adm_params, sizeof(adm_params));
  756. atomic_set(&rtac_adm_apr_data.cmd_state, 1);
  757. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
  758. __func__, opcode,
  759. &rtac_cal[ADM_RTAC_CAL].cal_data.paddr);
  760. result = apr_send_pkt(rtac_adm_apr_data.apr_handle,
  761. (uint32_t *)rtac_adm_buffer);
  762. if (result < 0) {
  763. pr_err("%s: Set params failed copp = %d\n", __func__, copp_id);
  764. goto err;
  765. }
  766. /* Wait for the callback */
  767. result = wait_event_timeout(rtac_adm_apr_data.cmd_wait,
  768. (atomic_read(&rtac_adm_apr_data.cmd_state) == 0),
  769. msecs_to_jiffies(TIMEOUT_MS));
  770. if (!result) {
  771. pr_err("%s: Set params timed out copp = %d\n", __func__,
  772. copp_id);
  773. goto err;
  774. }
  775. if (atomic_read(&rtac_common.apr_err_code)) {
  776. pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
  777. __func__, adsp_err_get_err_str(atomic_read(
  778. &rtac_common.apr_err_code)),
  779. opcode);
  780. result = adsp_err_get_lnx_err_code(
  781. atomic_read(
  782. &rtac_common.apr_err_code));
  783. goto err;
  784. }
  785. if (opcode == ADM_CMD_GET_PP_PARAMS_V5) {
  786. bytes_returned = ((u32 *)rtac_cal[ADM_RTAC_CAL].cal_data.
  787. kvaddr)[2] + 3 * sizeof(u32);
  788. if (bytes_returned > rtac_cal[ADM_RTAC_CAL].
  789. map_data.map_size) {
  790. pr_err("%s: Invalid data size = %d\n",
  791. __func__, bytes_returned);
  792. result = -EINVAL;
  793. goto err;
  794. }
  795. if (bytes_returned > user_buf_size) {
  796. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  797. __func__, user_buf_size, bytes_returned);
  798. result = -EINVAL;
  799. goto err;
  800. }
  801. if (copy_to_user(buf, (void *)
  802. rtac_cal[ADM_RTAC_CAL].cal_data.kvaddr,
  803. bytes_returned)) {
  804. pr_err("%s: Could not copy buffer to user,size = %d\n",
  805. __func__, bytes_returned);
  806. result = -EINVAL;
  807. goto err;
  808. }
  809. } else {
  810. bytes_returned = data_size;
  811. }
  812. mutex_unlock(&rtac_adm_apr_mutex);
  813. done:
  814. return bytes_returned;
  815. err:
  816. mutex_unlock(&rtac_adm_apr_mutex);
  817. return result;
  818. }
  819. /* ASM APR */
  820. void rtac_set_asm_handle(u32 session_id, void *handle)
  821. {
  822. pr_debug("%s\n", __func__);
  823. mutex_lock(&rtac_asm_apr_mutex);
  824. rtac_asm_apr_data[session_id].apr_handle = handle;
  825. mutex_unlock(&rtac_asm_apr_mutex);
  826. }
  827. bool rtac_make_asm_callback(u32 session_id, uint32_t *payload,
  828. u32 payload_size)
  829. {
  830. if (atomic_read(&rtac_asm_apr_data[session_id].cmd_state) != 1)
  831. return false;
  832. pr_debug("%s\n", __func__);
  833. if (payload_size == sizeof(uint32_t))
  834. atomic_set(&rtac_common.apr_err_code, payload[0]);
  835. else if (payload_size == (2*sizeof(uint32_t)))
  836. atomic_set(&rtac_common.apr_err_code, payload[1]);
  837. atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 0);
  838. wake_up(&rtac_asm_apr_data[session_id].cmd_wait);
  839. return true;
  840. }
  841. int send_rtac_asm_apr(void *buf, u32 opcode)
  842. {
  843. s32 result;
  844. u32 user_buf_size = 0;
  845. u32 bytes_returned = 0;
  846. u32 session_id = 0;
  847. u32 payload_size;
  848. u32 data_size = 0;
  849. struct apr_hdr asm_params;
  850. pr_debug("%s\n", __func__);
  851. if (rtac_cal[ASM_RTAC_CAL].map_data.ion_handle == NULL) {
  852. result = rtac_allocate_cal_buffer(ASM_RTAC_CAL);
  853. if (result < 0) {
  854. pr_err("%s: allocate buffer failed!",
  855. __func__);
  856. goto done;
  857. }
  858. }
  859. if (rtac_cal[ASM_RTAC_CAL].map_data.map_handle == 0) {
  860. result = rtac_map_cal_buffer(ASM_RTAC_CAL);
  861. if (result < 0) {
  862. pr_err("%s: map buffer failed!",
  863. __func__);
  864. goto done;
  865. }
  866. }
  867. if (copy_from_user(&user_buf_size, (void *)buf,
  868. sizeof(user_buf_size))) {
  869. pr_err("%s: Copy from user failed! buf = 0x%pK\n",
  870. __func__, buf);
  871. goto done;
  872. }
  873. if (user_buf_size <= 0) {
  874. pr_err("%s: Invalid buffer size = %d\n",
  875. __func__, user_buf_size);
  876. goto done;
  877. }
  878. if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
  879. pr_err("%s: Could not copy payload size from user buffer\n",
  880. __func__);
  881. goto done;
  882. }
  883. if (copy_from_user(&session_id, buf + 2 * sizeof(u32), sizeof(u32))) {
  884. pr_err("%s: Could not copy session id from user buffer\n",
  885. __func__);
  886. goto done;
  887. }
  888. if (session_id >= (ASM_ACTIVE_STREAMS_ALLOWED + 1)) {
  889. pr_err("%s: Invalid Session = %d\n", __func__, session_id);
  890. goto done;
  891. }
  892. mutex_lock(&rtac_asm_apr_mutex);
  893. if (rtac_asm_apr_data[session_id].apr_handle == NULL) {
  894. pr_err("%s: APR not initialized\n", __func__);
  895. result = -EINVAL;
  896. goto err;
  897. }
  898. if (opcode == ASM_STREAM_CMD_SET_PP_PARAMS_V2) {
  899. /* set payload size to in-band payload */
  900. /* set data size to actual out of band payload size */
  901. data_size = payload_size - 4 * sizeof(u32);
  902. if (data_size > rtac_cal[ASM_RTAC_CAL].map_data.map_size) {
  903. pr_err("%s: Invalid data size = %d\n",
  904. __func__, data_size);
  905. result = -EINVAL;
  906. goto err;
  907. }
  908. payload_size = 4 * sizeof(u32);
  909. /* Copy buffer to out-of-band payload */
  910. if (copy_from_user((void *)
  911. rtac_cal[ASM_RTAC_CAL].cal_data.kvaddr,
  912. buf + 7 * sizeof(u32), data_size)) {
  913. pr_err("%s: Could not copy payload from user buffer\n",
  914. __func__);
  915. result = -EINVAL;
  916. goto err;
  917. }
  918. /* set payload size in packet */
  919. rtac_asm_buffer[8] = data_size;
  920. } else {
  921. if (payload_size > MAX_PAYLOAD_SIZE) {
  922. pr_err("%s: Invalid payload size = %d\n",
  923. __func__, payload_size);
  924. result = -EINVAL;
  925. goto err;
  926. }
  927. /* Copy buffer to in-band payload */
  928. if (copy_from_user(rtac_asm_buffer +
  929. sizeof(asm_params)/sizeof(u32),
  930. buf + 3 * sizeof(u32), payload_size)) {
  931. pr_err("%s: Could not copy payload from user buffer\n",
  932. __func__);
  933. result = -EINVAL;
  934. goto err;
  935. }
  936. }
  937. /* Pack header */
  938. asm_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  939. APR_HDR_LEN(20), APR_PKT_VER);
  940. asm_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  941. payload_size);
  942. asm_params.src_svc = q6asm_get_apr_service_id(session_id);
  943. asm_params.src_domain = APR_DOMAIN_APPS;
  944. asm_params.src_port = (session_id << 8) | 0x0001;
  945. asm_params.dest_svc = APR_SVC_ASM;
  946. asm_params.dest_domain = APR_DOMAIN_ADSP;
  947. asm_params.dest_port = (session_id << 8) | 0x0001;
  948. asm_params.token = session_id;
  949. asm_params.opcode = opcode;
  950. /* fill for out-of-band */
  951. rtac_asm_buffer[5] =
  952. lower_32_bits(rtac_cal[ASM_RTAC_CAL].cal_data.paddr);
  953. rtac_asm_buffer[6] =
  954. msm_audio_populate_upper_32_bits(
  955. rtac_cal[ASM_RTAC_CAL].cal_data.paddr);
  956. rtac_asm_buffer[7] = rtac_cal[ASM_RTAC_CAL].map_data.map_handle;
  957. memcpy(rtac_asm_buffer, &asm_params, sizeof(asm_params));
  958. atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 1);
  959. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
  960. __func__, opcode,
  961. &rtac_cal[ASM_RTAC_CAL].cal_data.paddr);
  962. result = apr_send_pkt(rtac_asm_apr_data[session_id].apr_handle,
  963. (uint32_t *)rtac_asm_buffer);
  964. if (result < 0) {
  965. pr_err("%s: Set params failed session = %d\n",
  966. __func__, session_id);
  967. goto err;
  968. }
  969. /* Wait for the callback */
  970. result = wait_event_timeout(rtac_asm_apr_data[session_id].cmd_wait,
  971. (atomic_read(&rtac_asm_apr_data[session_id].cmd_state) == 0),
  972. 5 * HZ);
  973. if (!result) {
  974. pr_err("%s: Set params timed out session = %d\n",
  975. __func__, session_id);
  976. goto err;
  977. }
  978. if (atomic_read(&rtac_common.apr_err_code)) {
  979. pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
  980. __func__, adsp_err_get_err_str(atomic_read(
  981. &rtac_common.apr_err_code)),
  982. opcode);
  983. result = adsp_err_get_lnx_err_code(
  984. atomic_read(
  985. &rtac_common.apr_err_code));
  986. goto err;
  987. }
  988. if (opcode == ASM_STREAM_CMD_GET_PP_PARAMS_V2) {
  989. bytes_returned = ((u32 *)rtac_cal[ASM_RTAC_CAL].cal_data.
  990. kvaddr)[2] + 3 * sizeof(u32);
  991. if (bytes_returned > rtac_cal[ASM_RTAC_CAL].
  992. map_data.map_size) {
  993. pr_err("%s: Invalid data size = %d\n",
  994. __func__, bytes_returned);
  995. result = -EINVAL;
  996. goto err;
  997. }
  998. if (bytes_returned > user_buf_size) {
  999. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  1000. __func__, user_buf_size, bytes_returned);
  1001. result = -EINVAL;
  1002. goto err;
  1003. }
  1004. if (copy_to_user(buf, (void *)
  1005. rtac_cal[ASM_RTAC_CAL].cal_data.kvaddr,
  1006. bytes_returned)) {
  1007. pr_err("%s: Could not copy buffer to user,size = %d\n",
  1008. __func__, bytes_returned);
  1009. result = -EINVAL;
  1010. goto err;
  1011. }
  1012. } else {
  1013. bytes_returned = data_size;
  1014. }
  1015. mutex_unlock(&rtac_asm_apr_mutex);
  1016. done:
  1017. return bytes_returned;
  1018. err:
  1019. mutex_unlock(&rtac_asm_apr_mutex);
  1020. return result;
  1021. }
  1022. /* AFE APR */
  1023. void rtac_set_afe_handle(void *handle)
  1024. {
  1025. mutex_lock(&rtac_afe_apr_mutex);
  1026. rtac_afe_apr_data.apr_handle = handle;
  1027. mutex_unlock(&rtac_afe_apr_mutex);
  1028. }
  1029. bool rtac_make_afe_callback(uint32_t *payload, uint32_t payload_size)
  1030. {
  1031. pr_debug("%s:cmd_state = %d\n", __func__,
  1032. atomic_read(&rtac_afe_apr_data.cmd_state));
  1033. if (atomic_read(&rtac_afe_apr_data.cmd_state) != 1)
  1034. return false;
  1035. if (payload_size == sizeof(uint32_t))
  1036. atomic_set(&rtac_common.apr_err_code, payload[0]);
  1037. else if (payload_size == (2*sizeof(uint32_t)))
  1038. atomic_set(&rtac_common.apr_err_code, payload[1]);
  1039. atomic_set(&rtac_afe_apr_data.cmd_state, 0);
  1040. wake_up(&rtac_afe_apr_data.cmd_wait);
  1041. return true;
  1042. }
  1043. static int fill_afe_apr_hdr(struct apr_hdr *apr_hdr, uint32_t port,
  1044. uint32_t opcode, uint32_t apr_msg_size)
  1045. {
  1046. if (apr_hdr == NULL) {
  1047. pr_err("%s: invalid APR pointer", __func__);
  1048. return -EINVAL;
  1049. }
  1050. apr_hdr->hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1051. APR_HDR_LEN(APR_HDR_SIZE), APR_PKT_VER);
  1052. apr_hdr->pkt_size = apr_msg_size;
  1053. apr_hdr->src_svc = APR_SVC_AFE;
  1054. apr_hdr->src_domain = APR_DOMAIN_APPS;
  1055. apr_hdr->src_port = 0;
  1056. apr_hdr->dest_svc = APR_SVC_AFE;
  1057. apr_hdr->dest_domain = APR_DOMAIN_ADSP;
  1058. apr_hdr->dest_port = 0;
  1059. apr_hdr->token = port;
  1060. apr_hdr->opcode = opcode;
  1061. return 0;
  1062. }
  1063. static int send_rtac_afe_apr(void *buf, uint32_t opcode)
  1064. {
  1065. int32_t result;
  1066. uint32_t bytes_returned = 0;
  1067. uint32_t port_index = 0;
  1068. uint32_t apr_msg_size = 0;
  1069. struct rtac_afe_user_data user_afe_buf;
  1070. pr_debug("%s\n", __func__);
  1071. if (rtac_cal[AFE_RTAC_CAL].map_data.ion_handle == NULL) {
  1072. result = rtac_allocate_cal_buffer(AFE_RTAC_CAL);
  1073. if (result < 0) {
  1074. pr_err("%s: allocate buffer failed! ret = %d\n",
  1075. __func__, result);
  1076. goto done;
  1077. }
  1078. }
  1079. if (rtac_cal[AFE_RTAC_CAL].map_data.map_handle == 0) {
  1080. result = rtac_map_cal_buffer(AFE_RTAC_CAL);
  1081. if (result < 0) {
  1082. pr_err("%s: map buffer failed! ret = %d\n",
  1083. __func__, result);
  1084. goto done;
  1085. }
  1086. }
  1087. if (copy_from_user(&user_afe_buf, (void *)buf,
  1088. sizeof(struct rtac_afe_user_data))) {
  1089. pr_err("%s: Copy from user failed! buf = 0x%pK\n",
  1090. __func__, buf);
  1091. goto done;
  1092. }
  1093. if (user_afe_buf.buf_size <= 0) {
  1094. pr_err("%s: Invalid buffer size = %d\n",
  1095. __func__, user_afe_buf.buf_size);
  1096. goto done;
  1097. }
  1098. port_index = q6audio_get_port_index(user_afe_buf.port_id);
  1099. if (port_index >= AFE_MAX_PORTS) {
  1100. pr_err("%s: Invalid AFE port = 0x%x\n",
  1101. __func__, user_afe_buf.port_id);
  1102. goto done;
  1103. }
  1104. mutex_lock(&rtac_afe_apr_mutex);
  1105. if (rtac_afe_apr_data.apr_handle == NULL) {
  1106. pr_err("%s: APR not initialized\n", __func__);
  1107. result = -EINVAL;
  1108. goto err;
  1109. }
  1110. if (opcode == AFE_PORT_CMD_SET_PARAM_V2) {
  1111. struct afe_port_cmd_set_param_v2 *afe_set_apr_msg;
  1112. /* set data size to actual out of band payload size */
  1113. if (user_afe_buf.rtac_afe_set.cmd.payload_size >
  1114. rtac_cal[AFE_RTAC_CAL].map_data.map_size) {
  1115. pr_err("%s: Invalid data size = %d\n",
  1116. __func__,
  1117. user_afe_buf.rtac_afe_set.cmd.payload_size);
  1118. result = -EINVAL;
  1119. goto err;
  1120. }
  1121. /* Copy buffer to out-of-band payload */
  1122. if (copy_from_user((void *)
  1123. rtac_cal[AFE_RTAC_CAL].cal_data.kvaddr,
  1124. buf+offsetof(struct rtac_afe_user_data,
  1125. rtac_afe_set.data),
  1126. user_afe_buf.rtac_afe_set.cmd.payload_size)) {
  1127. pr_err("%s: Could not copy payload from user buffer\n",
  1128. __func__);
  1129. result = -EINVAL;
  1130. goto err;
  1131. }
  1132. /* Copy AFE APR Message */
  1133. afe_set_apr_msg = (struct afe_port_cmd_set_param_v2 *)
  1134. ((u8 *)rtac_afe_buffer +
  1135. sizeof(struct apr_hdr));
  1136. if (copy_from_user((void *)
  1137. afe_set_apr_msg,
  1138. buf + offsetof(struct rtac_afe_user_data,
  1139. rtac_afe_set.cmd),
  1140. sizeof(struct afe_port_cmd_set_param_v2))) {
  1141. pr_err("%s: Could not copy payload from user buffer\n",
  1142. __func__);
  1143. result = -EINVAL;
  1144. goto err;
  1145. }
  1146. afe_set_apr_msg->payload_address_lsw =
  1147. lower_32_bits(rtac_cal[AFE_RTAC_CAL].cal_data.paddr);
  1148. afe_set_apr_msg->payload_address_msw =
  1149. msm_audio_populate_upper_32_bits(
  1150. rtac_cal[AFE_RTAC_CAL].cal_data.paddr);
  1151. afe_set_apr_msg->mem_map_handle =
  1152. rtac_cal[AFE_RTAC_CAL].map_data.map_handle;
  1153. apr_msg_size = sizeof(struct apr_hdr) +
  1154. sizeof(struct afe_port_cmd_set_param_v2);
  1155. } else {
  1156. struct afe_port_cmd_get_param_v2 *afe_get_apr_msg;
  1157. if (user_afe_buf.cmd_size > MAX_PAYLOAD_SIZE) {
  1158. pr_err("%s: Invalid payload size = %d\n",
  1159. __func__, user_afe_buf.cmd_size);
  1160. result = -EINVAL;
  1161. goto err;
  1162. }
  1163. /* Copy buffer to in-band payload */
  1164. afe_get_apr_msg = (struct afe_port_cmd_get_param_v2 *)
  1165. ((u8 *) rtac_afe_buffer +
  1166. sizeof(struct apr_hdr));
  1167. if (copy_from_user((void *)afe_get_apr_msg,
  1168. buf+offsetof(struct rtac_afe_user_data,
  1169. rtac_afe_get.cmd),
  1170. sizeof(struct afe_port_cmd_get_param_v2))) {
  1171. pr_err("%s: Could not copy payload from user buffer\n",
  1172. __func__);
  1173. result = -EINVAL;
  1174. goto err;
  1175. }
  1176. afe_get_apr_msg->payload_address_lsw =
  1177. lower_32_bits(rtac_cal[AFE_RTAC_CAL].cal_data.paddr);
  1178. afe_get_apr_msg->payload_address_msw =
  1179. msm_audio_populate_upper_32_bits(
  1180. rtac_cal[AFE_RTAC_CAL].cal_data.paddr);
  1181. afe_get_apr_msg->mem_map_handle =
  1182. rtac_cal[AFE_RTAC_CAL].map_data.map_handle;
  1183. afe_get_apr_msg->payload_size -= sizeof(struct apr_hdr);
  1184. apr_msg_size = sizeof(struct apr_hdr) +
  1185. sizeof(struct afe_port_cmd_get_param_v2);
  1186. }
  1187. fill_afe_apr_hdr((struct apr_hdr *) rtac_afe_buffer,
  1188. port_index, opcode, apr_msg_size);
  1189. atomic_set(&rtac_afe_apr_data.cmd_state, 1);
  1190. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
  1191. __func__, opcode,
  1192. &rtac_cal[AFE_RTAC_CAL].cal_data.paddr);
  1193. result = apr_send_pkt(rtac_afe_apr_data.apr_handle,
  1194. (uint32_t *)rtac_afe_buffer);
  1195. if (result < 0) {
  1196. pr_err("%s: Set params failed port = 0x%x, ret = %d\n",
  1197. __func__, user_afe_buf.port_id, result);
  1198. goto err;
  1199. }
  1200. /* Wait for the callback */
  1201. result = wait_event_timeout(rtac_afe_apr_data.cmd_wait,
  1202. (atomic_read(&rtac_afe_apr_data.cmd_state) == 0),
  1203. msecs_to_jiffies(TIMEOUT_MS));
  1204. if (!result) {
  1205. pr_err("%s: Set params timed out port = 0x%x, ret = %d\n",
  1206. __func__, user_afe_buf.port_id, result);
  1207. goto err;
  1208. }
  1209. if (atomic_read(&rtac_common.apr_err_code)) {
  1210. pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
  1211. __func__, adsp_err_get_err_str(atomic_read(
  1212. &rtac_common.apr_err_code)),
  1213. opcode);
  1214. result = adsp_err_get_lnx_err_code(
  1215. atomic_read(
  1216. &rtac_common.apr_err_code));
  1217. goto err;
  1218. }
  1219. if (opcode == AFE_PORT_CMD_GET_PARAM_V2) {
  1220. struct afe_port_param_data_v2 *get_resp;
  1221. get_resp = (struct afe_port_param_data_v2 *)
  1222. rtac_cal[AFE_RTAC_CAL].cal_data.kvaddr;
  1223. bytes_returned = get_resp->param_size +
  1224. sizeof(struct afe_port_param_data_v2);
  1225. if (bytes_returned > rtac_cal[AFE_RTAC_CAL].
  1226. map_data.map_size) {
  1227. pr_err("%s: Invalid data size = %d\n",
  1228. __func__, bytes_returned);
  1229. result = -EINVAL;
  1230. goto err;
  1231. }
  1232. if (bytes_returned > user_afe_buf.buf_size) {
  1233. pr_err("%s: user size = 0x%x, returned size = 0x%x\n",
  1234. __func__, user_afe_buf.buf_size,
  1235. bytes_returned);
  1236. result = -EINVAL;
  1237. goto err;
  1238. }
  1239. if (copy_to_user(buf, (void *)
  1240. rtac_cal[AFE_RTAC_CAL].cal_data.kvaddr,
  1241. bytes_returned)) {
  1242. pr_err("%s: Could not copy buffer to user,size = %d\n",
  1243. __func__, bytes_returned);
  1244. result = -EINVAL;
  1245. goto err;
  1246. }
  1247. } else {
  1248. bytes_returned = user_afe_buf.rtac_afe_set.cmd.payload_size;
  1249. }
  1250. mutex_unlock(&rtac_afe_apr_mutex);
  1251. done:
  1252. return bytes_returned;
  1253. err:
  1254. mutex_unlock(&rtac_afe_apr_mutex);
  1255. return result;
  1256. }
  1257. /* Voice APR */
  1258. void rtac_set_voice_handle(u32 mode, void *handle)
  1259. {
  1260. pr_debug("%s\n", __func__);
  1261. mutex_lock(&rtac_voice_apr_mutex);
  1262. rtac_voice_apr_data[mode].apr_handle = handle;
  1263. mutex_unlock(&rtac_voice_apr_mutex);
  1264. }
  1265. bool rtac_make_voice_callback(u32 mode, uint32_t *payload, u32 payload_size)
  1266. {
  1267. if ((atomic_read(&rtac_voice_apr_data[mode].cmd_state) != 1) ||
  1268. (mode >= RTAC_VOICE_MODES))
  1269. return false;
  1270. pr_debug("%s\n", __func__);
  1271. if (payload_size == sizeof(uint32_t))
  1272. atomic_set(&rtac_common.apr_err_code, payload[0]);
  1273. else if (payload_size == (2*sizeof(uint32_t)))
  1274. atomic_set(&rtac_common.apr_err_code, payload[1]);
  1275. atomic_set(&rtac_voice_apr_data[mode].cmd_state, 0);
  1276. wake_up(&rtac_voice_apr_data[mode].cmd_wait);
  1277. return true;
  1278. }
  1279. int send_voice_apr(u32 mode, void *buf, u32 opcode)
  1280. {
  1281. s32 result;
  1282. u32 user_buf_size = 0;
  1283. u32 bytes_returned = 0;
  1284. u32 payload_size;
  1285. u32 dest_port;
  1286. u32 data_size = 0;
  1287. struct apr_hdr voice_params;
  1288. pr_debug("%s\n", __func__);
  1289. if (rtac_cal[VOICE_RTAC_CAL].map_data.ion_handle == NULL) {
  1290. result = rtac_allocate_cal_buffer(VOICE_RTAC_CAL);
  1291. if (result < 0) {
  1292. pr_err("%s: allocate buffer failed!",
  1293. __func__);
  1294. goto done;
  1295. }
  1296. }
  1297. if (rtac_cal[VOICE_RTAC_CAL].map_data.map_handle == 0) {
  1298. result = rtac_map_cal_buffer(VOICE_RTAC_CAL);
  1299. if (result < 0) {
  1300. pr_err("%s: map buffer failed!",
  1301. __func__);
  1302. goto done;
  1303. }
  1304. }
  1305. if (copy_from_user(&user_buf_size, (void *)buf,
  1306. sizeof(user_buf_size))) {
  1307. pr_err("%s: Copy from user failed! buf = 0x%pK\n",
  1308. __func__, buf);
  1309. goto done;
  1310. }
  1311. if (user_buf_size <= 0) {
  1312. pr_err("%s: Invalid buffer size = %d\n",
  1313. __func__, user_buf_size);
  1314. goto done;
  1315. }
  1316. if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
  1317. pr_err("%s: Could not copy payload size from user buffer\n",
  1318. __func__);
  1319. goto done;
  1320. }
  1321. if (copy_from_user(&dest_port, buf + 2 * sizeof(u32), sizeof(u32))) {
  1322. pr_err("%s: Could not copy port id from user buffer\n",
  1323. __func__);
  1324. goto done;
  1325. }
  1326. if ((mode != RTAC_CVP) && (mode != RTAC_CVS)) {
  1327. pr_err("%s: Invalid Mode for APR, mode = %d\n",
  1328. __func__, mode);
  1329. goto done;
  1330. }
  1331. mutex_lock(&rtac_voice_apr_mutex);
  1332. if (rtac_voice_apr_data[mode].apr_handle == NULL) {
  1333. pr_err("%s: APR not initialized\n", __func__);
  1334. result = -EINVAL;
  1335. goto err;
  1336. }
  1337. if (opcode == VSS_ICOMMON_CMD_SET_PARAM_V2) {
  1338. /* set payload size to in-band payload */
  1339. /* set data size to actual out of band payload size */
  1340. data_size = payload_size - 4 * sizeof(u32);
  1341. if (data_size > rtac_cal[VOICE_RTAC_CAL].map_data.map_size) {
  1342. pr_err("%s: Invalid data size = %d\n",
  1343. __func__, data_size);
  1344. result = -EINVAL;
  1345. goto err;
  1346. }
  1347. payload_size = 4 * sizeof(u32);
  1348. /* Copy buffer to out-of-band payload */
  1349. if (copy_from_user((void *)
  1350. rtac_cal[VOICE_RTAC_CAL].cal_data.kvaddr,
  1351. buf + 7 * sizeof(u32), data_size)) {
  1352. pr_err("%s: Could not copy payload from user buffer\n",
  1353. __func__);
  1354. result = -EINVAL;
  1355. goto err;
  1356. }
  1357. /* set payload size in packet */
  1358. rtac_voice_buffer[8] = data_size;
  1359. } else {
  1360. if (payload_size > MAX_PAYLOAD_SIZE) {
  1361. pr_err("%s: Invalid payload size = %d\n",
  1362. __func__, payload_size);
  1363. result = -EINVAL;
  1364. goto err;
  1365. }
  1366. /* Copy buffer to in-band payload */
  1367. if (copy_from_user(rtac_voice_buffer +
  1368. sizeof(voice_params)/sizeof(u32),
  1369. buf + 3 * sizeof(u32), payload_size)) {
  1370. pr_err("%s: Could not copy payload from user buffer\n",
  1371. __func__);
  1372. result = -EINVAL;
  1373. goto err;
  1374. }
  1375. }
  1376. /* Pack header */
  1377. voice_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1378. APR_HDR_LEN(20), APR_PKT_VER);
  1379. voice_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1380. payload_size);
  1381. voice_params.src_svc = 0;
  1382. voice_params.src_domain = APR_DOMAIN_APPS;
  1383. voice_params.src_port = get_voice_index(mode, dest_port);
  1384. voice_params.dest_svc = 0;
  1385. voice_params.dest_domain = APR_DOMAIN_MODEM;
  1386. voice_params.dest_port = (u16)dest_port;
  1387. voice_params.token = (opcode == VSS_ICOMMON_CMD_SET_PARAM_V2) ?
  1388. VOC_RTAC_SET_PARAM_TOKEN :
  1389. 0;
  1390. voice_params.opcode = opcode;
  1391. /* fill for out-of-band */
  1392. rtac_voice_buffer[5] = rtac_cal[VOICE_RTAC_CAL].map_data.map_handle;
  1393. rtac_voice_buffer[6] =
  1394. lower_32_bits(rtac_cal[VOICE_RTAC_CAL].cal_data.paddr);
  1395. rtac_voice_buffer[7] =
  1396. msm_audio_populate_upper_32_bits(
  1397. rtac_cal[VOICE_RTAC_CAL].cal_data.paddr);
  1398. memcpy(rtac_voice_buffer, &voice_params, sizeof(voice_params));
  1399. atomic_set(&rtac_voice_apr_data[mode].cmd_state, 1);
  1400. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%pK\n",
  1401. __func__, opcode,
  1402. &rtac_cal[VOICE_RTAC_CAL].cal_data.paddr);
  1403. result = apr_send_pkt(rtac_voice_apr_data[mode].apr_handle,
  1404. (uint32_t *)rtac_voice_buffer);
  1405. if (result < 0) {
  1406. pr_err("%s: apr_send_pkt failed opcode = %x\n",
  1407. __func__, opcode);
  1408. goto err;
  1409. }
  1410. /* Wait for the callback */
  1411. result = wait_event_timeout(rtac_voice_apr_data[mode].cmd_wait,
  1412. (atomic_read(&rtac_voice_apr_data[mode].cmd_state) == 0),
  1413. msecs_to_jiffies(TIMEOUT_MS));
  1414. if (!result) {
  1415. pr_err("%s: apr_send_pkt timed out opcode = %x\n",
  1416. __func__, opcode);
  1417. goto err;
  1418. }
  1419. if (atomic_read(&rtac_common.apr_err_code)) {
  1420. pr_err("%s: DSP returned error code = [%s], opcode = 0x%x\n",
  1421. __func__, adsp_err_get_err_str(atomic_read(
  1422. &rtac_common.apr_err_code)),
  1423. opcode);
  1424. result = adsp_err_get_lnx_err_code(
  1425. atomic_read(
  1426. &rtac_common.apr_err_code));
  1427. goto err;
  1428. }
  1429. if (opcode == VSS_ICOMMON_CMD_GET_PARAM_V2) {
  1430. bytes_returned = ((u32 *)rtac_cal[VOICE_RTAC_CAL].cal_data.
  1431. kvaddr)[2] + 3 * sizeof(u32);
  1432. if (bytes_returned > rtac_cal[VOICE_RTAC_CAL].
  1433. map_data.map_size) {
  1434. pr_err("%s: Invalid data size = %d\n",
  1435. __func__, bytes_returned);
  1436. result = -EINVAL;
  1437. goto err;
  1438. }
  1439. if (bytes_returned > user_buf_size) {
  1440. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  1441. __func__, user_buf_size, bytes_returned);
  1442. result = -EINVAL;
  1443. goto err;
  1444. }
  1445. if (copy_to_user(buf, (void *)
  1446. rtac_cal[VOICE_RTAC_CAL].cal_data.kvaddr,
  1447. bytes_returned)) {
  1448. pr_err("%s: Could not copy buffer to user, size = %d\n",
  1449. __func__, bytes_returned);
  1450. result = -EINVAL;
  1451. goto err;
  1452. }
  1453. } else {
  1454. bytes_returned = data_size;
  1455. }
  1456. mutex_unlock(&rtac_voice_apr_mutex);
  1457. done:
  1458. return bytes_returned;
  1459. err:
  1460. mutex_unlock(&rtac_voice_apr_mutex);
  1461. return result;
  1462. }
  1463. void get_rtac_adm_data(struct rtac_adm *adm_data)
  1464. {
  1465. mutex_lock(&rtac_adm_mutex);
  1466. memcpy(adm_data, &rtac_adm_data, sizeof(struct rtac_adm));
  1467. mutex_unlock(&rtac_adm_mutex);
  1468. }
  1469. static long rtac_ioctl_shared(struct file *f,
  1470. unsigned int cmd, void *arg)
  1471. {
  1472. int result = 0;
  1473. if (!arg) {
  1474. pr_err("%s: No data sent to driver!\n", __func__);
  1475. result = -EFAULT;
  1476. goto done;
  1477. }
  1478. switch (cmd) {
  1479. case AUDIO_GET_RTAC_ADM_INFO: {
  1480. mutex_lock(&rtac_adm_mutex);
  1481. if (copy_to_user((void *)arg, &rtac_adm_data,
  1482. sizeof(rtac_adm_data))) {
  1483. pr_err("%s: copy_to_user failed for AUDIO_GET_RTAC_ADM_INFO\n",
  1484. __func__);
  1485. mutex_unlock(&rtac_adm_mutex);
  1486. return -EFAULT;
  1487. }
  1488. result = sizeof(rtac_adm_data);
  1489. mutex_unlock(&rtac_adm_mutex);
  1490. break;
  1491. }
  1492. case AUDIO_GET_RTAC_VOICE_INFO: {
  1493. mutex_lock(&rtac_voice_mutex);
  1494. if (copy_to_user((void *)arg, &rtac_voice_data,
  1495. sizeof(rtac_voice_data))) {
  1496. pr_err("%s: copy_to_user failed for AUDIO_GET_RTAC_VOICE_INFO\n",
  1497. __func__);
  1498. mutex_unlock(&rtac_voice_mutex);
  1499. return -EFAULT;
  1500. }
  1501. result = sizeof(rtac_voice_data);
  1502. mutex_unlock(&rtac_voice_mutex);
  1503. break;
  1504. }
  1505. case AUDIO_GET_RTAC_ADM_CAL:
  1506. result = send_adm_apr((void *)arg, ADM_CMD_GET_PP_PARAMS_V5);
  1507. break;
  1508. case AUDIO_SET_RTAC_ADM_CAL:
  1509. result = send_adm_apr((void *)arg, ADM_CMD_SET_PP_PARAMS_V5);
  1510. break;
  1511. case AUDIO_GET_RTAC_ASM_CAL:
  1512. result = send_rtac_asm_apr((void *)arg,
  1513. ASM_STREAM_CMD_GET_PP_PARAMS_V2);
  1514. break;
  1515. case AUDIO_SET_RTAC_ASM_CAL:
  1516. result = send_rtac_asm_apr((void *)arg,
  1517. ASM_STREAM_CMD_SET_PP_PARAMS_V2);
  1518. break;
  1519. case AUDIO_GET_RTAC_CVS_CAL:
  1520. result = send_voice_apr(RTAC_CVS, (void *) arg,
  1521. VSS_ICOMMON_CMD_GET_PARAM_V2);
  1522. break;
  1523. case AUDIO_SET_RTAC_CVS_CAL:
  1524. result = send_voice_apr(RTAC_CVS, (void *) arg,
  1525. VSS_ICOMMON_CMD_SET_PARAM_V2);
  1526. break;
  1527. case AUDIO_GET_RTAC_CVP_CAL:
  1528. result = send_voice_apr(RTAC_CVP, (void *) arg,
  1529. VSS_ICOMMON_CMD_GET_PARAM_V2);
  1530. break;
  1531. case AUDIO_SET_RTAC_CVP_CAL:
  1532. result = send_voice_apr(RTAC_CVP, (void *) arg,
  1533. VSS_ICOMMON_CMD_SET_PARAM_V2);
  1534. break;
  1535. case AUDIO_GET_RTAC_AFE_CAL:
  1536. result = send_rtac_afe_apr((void *)arg,
  1537. AFE_PORT_CMD_GET_PARAM_V2);
  1538. break;
  1539. case AUDIO_SET_RTAC_AFE_CAL:
  1540. result = send_rtac_afe_apr((void *)arg,
  1541. AFE_PORT_CMD_SET_PARAM_V2);
  1542. break;
  1543. default:
  1544. pr_err("%s: Invalid IOCTL, command = %d!\n",
  1545. __func__, cmd);
  1546. result = -EINVAL;
  1547. }
  1548. done:
  1549. return result;
  1550. }
  1551. static long rtac_ioctl(struct file *f,
  1552. unsigned int cmd, unsigned long arg)
  1553. {
  1554. int result = 0;
  1555. if (!arg) {
  1556. pr_err("%s: No data sent to driver!\n", __func__);
  1557. result = -EFAULT;
  1558. } else {
  1559. result = rtac_ioctl_shared(f, cmd, (void __user *)arg);
  1560. }
  1561. return result;
  1562. }
  1563. #ifdef CONFIG_COMPAT
  1564. #define AUDIO_GET_RTAC_ADM_INFO_32 _IOR(CAL_IOCTL_MAGIC, 207, compat_uptr_t)
  1565. #define AUDIO_GET_RTAC_VOICE_INFO_32 _IOR(CAL_IOCTL_MAGIC, 208, compat_uptr_t)
  1566. #define AUDIO_GET_RTAC_ADM_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 209, compat_uptr_t)
  1567. #define AUDIO_SET_RTAC_ADM_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 210, compat_uptr_t)
  1568. #define AUDIO_GET_RTAC_ASM_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 211, compat_uptr_t)
  1569. #define AUDIO_SET_RTAC_ASM_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 212, compat_uptr_t)
  1570. #define AUDIO_GET_RTAC_CVS_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 213, compat_uptr_t)
  1571. #define AUDIO_SET_RTAC_CVS_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 214, compat_uptr_t)
  1572. #define AUDIO_GET_RTAC_CVP_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 215, compat_uptr_t)
  1573. #define AUDIO_SET_RTAC_CVP_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 216, compat_uptr_t)
  1574. #define AUDIO_GET_RTAC_AFE_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 217, compat_uptr_t)
  1575. #define AUDIO_SET_RTAC_AFE_CAL_32 _IOWR(CAL_IOCTL_MAGIC, 218, compat_uptr_t)
  1576. static long rtac_compat_ioctl(struct file *f,
  1577. unsigned int cmd, unsigned long arg)
  1578. {
  1579. int result = 0;
  1580. if (!arg) {
  1581. pr_err("%s: No data sent to driver!\n", __func__);
  1582. result = -EINVAL;
  1583. goto done;
  1584. }
  1585. switch (cmd) {
  1586. case AUDIO_GET_RTAC_ADM_INFO_32:
  1587. cmd = AUDIO_GET_RTAC_ADM_INFO;
  1588. goto process;
  1589. case AUDIO_GET_RTAC_VOICE_INFO_32:
  1590. cmd = AUDIO_GET_RTAC_VOICE_INFO;
  1591. goto process;
  1592. case AUDIO_GET_RTAC_AFE_CAL_32:
  1593. cmd = AUDIO_GET_RTAC_AFE_CAL;
  1594. goto process;
  1595. case AUDIO_SET_RTAC_AFE_CAL_32:
  1596. cmd = AUDIO_SET_RTAC_AFE_CAL;
  1597. goto process;
  1598. case AUDIO_GET_RTAC_ADM_CAL_32:
  1599. cmd = AUDIO_GET_RTAC_ADM_CAL;
  1600. goto process;
  1601. case AUDIO_SET_RTAC_ADM_CAL_32:
  1602. cmd = AUDIO_SET_RTAC_ADM_CAL;
  1603. goto process;
  1604. case AUDIO_GET_RTAC_ASM_CAL_32:
  1605. cmd = AUDIO_GET_RTAC_ASM_CAL;
  1606. goto process;
  1607. case AUDIO_SET_RTAC_ASM_CAL_32:
  1608. cmd = AUDIO_SET_RTAC_ASM_CAL;
  1609. goto process;
  1610. case AUDIO_GET_RTAC_CVS_CAL_32:
  1611. cmd = AUDIO_GET_RTAC_CVS_CAL;
  1612. goto process;
  1613. case AUDIO_SET_RTAC_CVS_CAL_32:
  1614. cmd = AUDIO_SET_RTAC_CVS_CAL;
  1615. goto process;
  1616. case AUDIO_GET_RTAC_CVP_CAL_32:
  1617. cmd = AUDIO_GET_RTAC_CVP_CAL;
  1618. goto process;
  1619. case AUDIO_SET_RTAC_CVP_CAL_32:
  1620. cmd = AUDIO_SET_RTAC_CVP_CAL;
  1621. process:
  1622. result = rtac_ioctl_shared(f, cmd, compat_ptr(arg));
  1623. break;
  1624. default:
  1625. result = -EINVAL;
  1626. pr_err("%s: Invalid IOCTL, command = %d!\n",
  1627. __func__, cmd);
  1628. break;
  1629. }
  1630. done:
  1631. return result;
  1632. }
  1633. #else
  1634. #define rtac_compat_ioctl NULL
  1635. #endif
  1636. static const struct file_operations rtac_fops = {
  1637. .owner = THIS_MODULE,
  1638. .open = rtac_open,
  1639. .release = rtac_release,
  1640. .unlocked_ioctl = rtac_ioctl,
  1641. .compat_ioctl = rtac_compat_ioctl,
  1642. };
  1643. struct miscdevice rtac_misc = {
  1644. .minor = MISC_DYNAMIC_MINOR,
  1645. .name = "msm_rtac",
  1646. .fops = &rtac_fops,
  1647. };
  1648. int __init rtac_init(void)
  1649. {
  1650. int i = 0;
  1651. /* Driver */
  1652. atomic_set(&rtac_common.usage_count, 0);
  1653. atomic_set(&rtac_common.apr_err_code, 0);
  1654. /* ADM */
  1655. memset(&rtac_adm_data, 0, sizeof(rtac_adm_data));
  1656. rtac_adm_apr_data.apr_handle = NULL;
  1657. atomic_set(&rtac_adm_apr_data.cmd_state, 0);
  1658. init_waitqueue_head(&rtac_adm_apr_data.cmd_wait);
  1659. mutex_init(&rtac_adm_mutex);
  1660. mutex_init(&rtac_adm_apr_mutex);
  1661. rtac_adm_buffer = kzalloc(
  1662. rtac_cal[ADM_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1663. if (rtac_adm_buffer == NULL)
  1664. goto nomem;
  1665. /* ASM */
  1666. for (i = 0; i < ASM_ACTIVE_STREAMS_ALLOWED+1; i++) {
  1667. rtac_asm_apr_data[i].apr_handle = NULL;
  1668. atomic_set(&rtac_asm_apr_data[i].cmd_state, 0);
  1669. init_waitqueue_head(&rtac_asm_apr_data[i].cmd_wait);
  1670. }
  1671. mutex_init(&rtac_asm_apr_mutex);
  1672. rtac_asm_buffer = kzalloc(
  1673. rtac_cal[ASM_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1674. if (rtac_asm_buffer == NULL) {
  1675. kzfree(rtac_adm_buffer);
  1676. goto nomem;
  1677. }
  1678. /* AFE */
  1679. rtac_afe_apr_data.apr_handle = NULL;
  1680. atomic_set(&rtac_afe_apr_data.cmd_state, 0);
  1681. init_waitqueue_head(&rtac_afe_apr_data.cmd_wait);
  1682. mutex_init(&rtac_afe_apr_mutex);
  1683. rtac_afe_buffer = kzalloc(
  1684. rtac_cal[AFE_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1685. if (rtac_afe_buffer == NULL) {
  1686. kzfree(rtac_adm_buffer);
  1687. kzfree(rtac_asm_buffer);
  1688. goto nomem;
  1689. }
  1690. /* Voice */
  1691. memset(&rtac_voice_data, 0, sizeof(rtac_voice_data));
  1692. for (i = 0; i < RTAC_VOICE_MODES; i++) {
  1693. rtac_voice_apr_data[i].apr_handle = NULL;
  1694. atomic_set(&rtac_voice_apr_data[i].cmd_state, 0);
  1695. init_waitqueue_head(&rtac_voice_apr_data[i].cmd_wait);
  1696. }
  1697. mutex_init(&rtac_voice_mutex);
  1698. mutex_init(&rtac_voice_apr_mutex);
  1699. rtac_voice_buffer = kzalloc(
  1700. rtac_cal[VOICE_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1701. if (rtac_voice_buffer == NULL) {
  1702. kzfree(rtac_adm_buffer);
  1703. kzfree(rtac_asm_buffer);
  1704. kzfree(rtac_afe_buffer);
  1705. goto nomem;
  1706. }
  1707. return misc_register(&rtac_misc);
  1708. nomem:
  1709. return -ENOMEM;
  1710. }
  1711. void rtac_exit(void)
  1712. {
  1713. kzfree(rtac_adm_buffer);
  1714. kzfree(rtac_asm_buffer);
  1715. kzfree(rtac_afe_buffer);
  1716. misc_deregister(&rtac_misc);
  1717. }
  1718. MODULE_DESCRIPTION("SoC QDSP6v2 Real-Time Audio Calibration driver");
  1719. MODULE_LICENSE("GPL v2");