rtac.c 51 KB

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