rtac.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. /* Copyright (c) 2012-2015, 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_acdb.h>
  21. #include <linux/atomic.h>
  22. #include <linux/msm_audio_ion.h>
  23. #include <mach/qdsp6v2/rtac.h>
  24. #include <sound/q6asm-v2.h>
  25. #include <sound/q6afe-v2.h>
  26. #include <sound/q6audio-v2.h>
  27. #include <sound/apr_audio-v2.h>
  28. #include "q6voice.h"
  29. #include "audio_acdb.h"
  30. #ifndef CONFIG_RTAC
  31. void rtac_add_adm_device(u32 port_id, u32 copp_id, u32 path_id, u32 popp_id) {}
  32. void rtac_remove_adm_device(u32 port_id) {}
  33. void rtac_remove_popp_from_adm_devices(u32 popp_id) {}
  34. void rtac_set_adm_handle(void *handle) {}
  35. bool rtac_make_adm_callback(uint32_t *payload, u32 payload_size)
  36. {return false; }
  37. void rtac_set_asm_handle(u32 session_id, void *handle) {}
  38. bool rtac_make_asm_callback(u32 session_id, uint32_t *payload,
  39. u32 payload_size) {return false; }
  40. void rtac_add_voice(u32 cvs_handle, u32 cvp_handle, u32 rx_afe_port,
  41. u32 tx_afe_port, u32 session_id) {}
  42. void rtac_remove_voice(u32 cvs_handle) {}
  43. void rtac_set_voice_handle(u32 mode, void *handle) {}
  44. bool rtac_make_voice_callback(u32 mode, uint32_t *payload,
  45. u32 payload_size) {return false; }
  46. #else
  47. /* Max size of payload (buf size - apr header) */
  48. #define MAX_PAYLOAD_SIZE 4076
  49. #define RTAC_MAX_ACTIVE_DEVICES 4
  50. #define RTAC_MAX_ACTIVE_VOICE_COMBOS 2
  51. #define RTAC_MAX_ACTIVE_POPP 8
  52. #define RTAC_BUF_SIZE 8192
  53. #define TIMEOUT_MS 1000
  54. struct rtac_cal_block_data rtac_cal[MAX_RTAC_BLOCKS] = {
  55. /* ADM_RTAC_CAL */
  56. {{RTAC_BUF_SIZE, 0, 0, 0}, {0, 0, 0} },
  57. /* ASM_RTAC_CAL */
  58. {{RTAC_BUF_SIZE, 0, 0, 0}, {0, 0, 0} },
  59. /* VOICE_RTAC_CAL */
  60. {{RTAC_BUF_SIZE, 0, 0, 0}, {0, 0, 0} }
  61. };
  62. struct rtac_common_data {
  63. atomic_t usage_count;
  64. atomic_t apr_err_code;
  65. };
  66. static struct rtac_common_data rtac_common;
  67. /* APR data */
  68. struct rtac_apr_data {
  69. void *apr_handle;
  70. atomic_t cmd_state;
  71. wait_queue_head_t cmd_wait;
  72. };
  73. static struct rtac_apr_data rtac_adm_apr_data;
  74. static struct rtac_apr_data rtac_asm_apr_data[SESSION_MAX+1];
  75. static struct rtac_apr_data rtac_voice_apr_data[RTAC_VOICE_MODES];
  76. /* ADM info & APR */
  77. struct rtac_adm_data {
  78. uint32_t topology_id;
  79. uint32_t afe_port;
  80. uint32_t copp;
  81. uint32_t num_of_popp;
  82. uint32_t popp[RTAC_MAX_ACTIVE_POPP];
  83. };
  84. struct rtac_adm {
  85. uint32_t num_of_dev;
  86. struct rtac_adm_data device[RTAC_MAX_ACTIVE_DEVICES];
  87. };
  88. static struct rtac_adm rtac_adm_data;
  89. /* ADM V2 data */
  90. struct rtac_popp_data {
  91. uint32_t popp;
  92. uint32_t popp_topology;
  93. };
  94. struct rtac_adm_data_v2 {
  95. uint32_t topology_id;
  96. uint32_t afe_port;
  97. uint32_t copp;
  98. uint32_t num_of_popp;
  99. struct rtac_popp_data popp[RTAC_MAX_ACTIVE_POPP];
  100. };
  101. struct rtac_adm_v2 {
  102. uint32_t num_of_dev;
  103. struct rtac_adm_data_v2 device[RTAC_MAX_ACTIVE_DEVICES];
  104. };
  105. static struct rtac_adm_v2 rtac_adm_data_v2;
  106. static u32 *rtac_adm_buffer;
  107. /* ASM APR */
  108. static u32 *rtac_asm_buffer;
  109. /* Voice info & APR */
  110. struct rtac_voice_data {
  111. uint32_t tx_topology_id;
  112. uint32_t rx_topology_id;
  113. uint32_t tx_afe_port;
  114. uint32_t rx_afe_port;
  115. uint16_t cvs_handle;
  116. uint16_t cvp_handle;
  117. };
  118. struct rtac_voice {
  119. uint32_t num_of_voice_combos;
  120. struct rtac_voice_data voice[RTAC_MAX_ACTIVE_VOICE_COMBOS];
  121. };
  122. static struct rtac_voice rtac_voice_data;
  123. static u32 *rtac_voice_buffer;
  124. static u32 voice_session_id[RTAC_MAX_ACTIVE_VOICE_COMBOS];
  125. struct mutex rtac_adm_mutex;
  126. struct mutex rtac_adm_apr_mutex;
  127. struct mutex rtac_asm_apr_mutex;
  128. struct mutex rtac_voice_mutex;
  129. struct mutex rtac_voice_apr_mutex;
  130. int rtac_clear_mapping(uint32_t cal_type)
  131. {
  132. int result = 0;
  133. pr_debug("%s\n", __func__);
  134. if (cal_type >= MAX_RTAC_BLOCKS) {
  135. pr_debug("%s: invalid cal type %d\n", __func__, cal_type);
  136. result = -EINVAL;
  137. goto done;
  138. }
  139. rtac_cal[cal_type].map_data.map_handle = 0;
  140. done:
  141. return result;
  142. }
  143. int rtac_allocate_cal_buffer(uint32_t cal_type)
  144. {
  145. int result = 0;
  146. int len;
  147. pr_debug("%s\n", __func__);
  148. if (cal_type >= MAX_RTAC_BLOCKS) {
  149. pr_err("%s: cal_type %d is invalid!\n",
  150. __func__, cal_type);
  151. result = -EINVAL;
  152. goto done;
  153. }
  154. if (rtac_cal[cal_type].cal_data.paddr != 0) {
  155. pr_err("%s: memory already allocated! cal_type %d, paddr 0x%x\n",
  156. __func__, cal_type, rtac_cal[cal_type].cal_data.paddr);
  157. result = -EPERM;
  158. goto done;
  159. }
  160. result = msm_audio_ion_alloc("rtac_client",
  161. &rtac_cal[cal_type].map_data.ion_client,
  162. &rtac_cal[cal_type].map_data.ion_handle,
  163. rtac_cal[cal_type].map_data.map_size,
  164. (ion_phys_addr_t *)&rtac_cal[cal_type].cal_data.paddr,
  165. (size_t *)&len,
  166. (void **)&rtac_cal[cal_type].cal_data.kvaddr);
  167. if (result < 0) {
  168. pr_err("%s: ION create client for RTAC failed\n",
  169. __func__);
  170. goto done;
  171. }
  172. pr_debug("%s: cal_type %d, paddr 0x%x, kvaddr 0x%x, map_size 0x%x\n",
  173. __func__, cal_type,
  174. rtac_cal[cal_type].cal_data.paddr,
  175. rtac_cal[cal_type].cal_data.kvaddr,
  176. rtac_cal[cal_type].map_data.map_size);
  177. done:
  178. return result;
  179. }
  180. int rtac_free_cal_buffer(uint32_t cal_type)
  181. {
  182. int result = 0;
  183. pr_debug("%s\n", __func__);
  184. if (cal_type >= MAX_RTAC_BLOCKS) {
  185. pr_err("%s: cal_type %d is invalid!\n",
  186. __func__, cal_type);
  187. result = -EINVAL;
  188. goto done;
  189. }
  190. if (rtac_cal[cal_type].map_data.ion_client == NULL) {
  191. pr_debug("%s: cal_type %d not allocated!\n",
  192. __func__, cal_type);
  193. goto done;
  194. }
  195. result = msm_audio_ion_free(rtac_cal[cal_type].map_data.ion_client,
  196. rtac_cal[cal_type].map_data.ion_handle);
  197. if (result < 0) {
  198. pr_err("%s: ION free for RTAC failed! cal_type %d, paddr 0x%x\n",
  199. __func__, cal_type, rtac_cal[cal_type].cal_data.paddr);
  200. goto done;
  201. }
  202. rtac_cal[cal_type].map_data.map_handle = 0;
  203. rtac_cal[cal_type].map_data.ion_client = NULL;
  204. rtac_cal[cal_type].map_data.ion_handle = NULL;
  205. rtac_cal[cal_type].cal_data.size = 0;
  206. rtac_cal[cal_type].cal_data.kvaddr = 0;
  207. rtac_cal[cal_type].cal_data.paddr = 0;
  208. done:
  209. return result;
  210. }
  211. int rtac_map_cal_buffer(uint32_t cal_type)
  212. {
  213. int result = 0;
  214. pr_debug("%s\n", __func__);
  215. if (cal_type >= MAX_RTAC_BLOCKS) {
  216. pr_err("%s: cal_type %d is invalid!\n",
  217. __func__, cal_type);
  218. result = -EINVAL;
  219. goto done;
  220. }
  221. if (rtac_cal[cal_type].map_data.map_handle != 0) {
  222. pr_err("%s: already mapped cal_type %d\n",
  223. __func__, cal_type);
  224. result = -EPERM;
  225. goto done;
  226. }
  227. if (rtac_cal[cal_type].cal_data.paddr == 0) {
  228. pr_err("%s: physical address is NULL cal_type %d\n",
  229. __func__, cal_type);
  230. result = -EPERM;
  231. goto done;
  232. }
  233. switch (cal_type) {
  234. case ADM_RTAC_CAL:
  235. result = adm_map_rtac_block(&rtac_cal[cal_type]);
  236. break;
  237. case ASM_RTAC_CAL:
  238. result = q6asm_map_rtac_block(&rtac_cal[cal_type]);
  239. break;
  240. case VOICE_RTAC_CAL:
  241. result = voc_map_rtac_block(&rtac_cal[cal_type]);
  242. break;
  243. }
  244. if (result < 0) {
  245. pr_err("%s: map RTAC failed! cal_type %d\n",
  246. __func__, cal_type);
  247. goto done;
  248. }
  249. done:
  250. return result;
  251. }
  252. int rtac_unmap_cal_buffer(uint32_t cal_type)
  253. {
  254. int result = 0;
  255. pr_debug("%s\n", __func__);
  256. if (cal_type >= MAX_RTAC_BLOCKS) {
  257. pr_err("%s: cal_type %d is invalid!\n",
  258. __func__, cal_type);
  259. result = -EINVAL;
  260. goto done;
  261. }
  262. if (rtac_cal[cal_type].map_data.map_handle == 0) {
  263. pr_debug("%s: nothing to unmap cal_type %d\n",
  264. __func__, cal_type);
  265. goto done;
  266. }
  267. switch (cal_type) {
  268. case ADM_RTAC_CAL:
  269. result = adm_unmap_rtac_block(
  270. &rtac_cal[cal_type].map_data.map_handle);
  271. break;
  272. case ASM_RTAC_CAL:
  273. result = q6asm_unmap_rtac_block(
  274. &rtac_cal[cal_type].map_data.map_handle);
  275. break;
  276. case VOICE_RTAC_CAL:
  277. result = voc_unmap_rtac_block(
  278. &rtac_cal[cal_type].map_data.map_handle);
  279. break;
  280. }
  281. if (result < 0) {
  282. pr_err("%s: unmap RTAC failed! cal_type %d\n",
  283. __func__, cal_type);
  284. goto done;
  285. }
  286. done:
  287. return result;
  288. }
  289. static int rtac_open(struct inode *inode, struct file *f)
  290. {
  291. int result = 0;
  292. pr_debug("%s\n", __func__);
  293. atomic_inc(&rtac_common.usage_count);
  294. return result;
  295. }
  296. static int rtac_release(struct inode *inode, struct file *f)
  297. {
  298. int result = 0;
  299. int result2 = 0;
  300. int i;
  301. pr_debug("%s\n", __func__);
  302. atomic_dec(&rtac_common.usage_count);
  303. pr_debug("%s: ref count %d!\n", __func__,
  304. atomic_read(&rtac_common.usage_count));
  305. if (atomic_read(&rtac_common.usage_count) > 0)
  306. goto done;
  307. for (i = 0; i < MAX_RTAC_BLOCKS; i++) {
  308. result2 = rtac_unmap_cal_buffer(i);
  309. if (result2 < 0) {
  310. pr_err("%s: unmap buffer failed! error %d!\n",
  311. __func__, result2);
  312. result = result2;
  313. }
  314. result2 = rtac_free_cal_buffer(i);
  315. if (result2 < 0) {
  316. pr_err("%s: free buffer failed! error %d!\n",
  317. __func__, result2);
  318. result = result2;
  319. }
  320. }
  321. done:
  322. return result;
  323. }
  324. /* ADM Info V2 */
  325. static void add_popp_v2(u32 dev_idx, u32 port_id, u32 popp_id)
  326. {
  327. u32 i = 0;
  328. for (; i < rtac_adm_data_v2.device[dev_idx].num_of_popp; i++)
  329. if (rtac_adm_data_v2.device[dev_idx].popp[i].popp == popp_id)
  330. goto done;
  331. if (rtac_adm_data_v2.device[dev_idx].num_of_popp ==
  332. RTAC_MAX_ACTIVE_POPP) {
  333. pr_err("%s, Max POPP!\n", __func__);
  334. goto done;
  335. }
  336. rtac_adm_data_v2.device[dev_idx].popp[
  337. rtac_adm_data_v2.device[dev_idx].num_of_popp].popp = popp_id;
  338. rtac_adm_data_v2.device[dev_idx].popp[
  339. rtac_adm_data_v2.device[dev_idx].num_of_popp++].popp_topology =
  340. get_asm_topology();
  341. done:
  342. return;
  343. }
  344. static void rtac_add_adm_device_v2(u32 port_id, u32 copp_id, u32 path_id,
  345. u32 popp_id)
  346. {
  347. u32 i = 0;
  348. pr_debug("%s: port_id = %d, popp_id = %d\n", __func__, port_id,
  349. popp_id);
  350. if (rtac_adm_data_v2.num_of_dev == RTAC_MAX_ACTIVE_DEVICES) {
  351. pr_err("%s, Can't add anymore RTAC devices!\n", __func__);
  352. goto done;
  353. }
  354. /* Check if device already added */
  355. if (rtac_adm_data_v2.num_of_dev != 0) {
  356. for (; i < rtac_adm_data_v2.num_of_dev; i++) {
  357. if (rtac_adm_data_v2.device[i].afe_port == port_id) {
  358. add_popp_v2(i, port_id, popp_id);
  359. goto done;
  360. }
  361. if (rtac_adm_data_v2.device[i].num_of_popp ==
  362. RTAC_MAX_ACTIVE_POPP) {
  363. pr_err("%s, Max POPP!\n", __func__);
  364. goto done;
  365. }
  366. }
  367. }
  368. /* Add device */
  369. rtac_adm_data_v2.num_of_dev++;
  370. if (path_id == ADM_PATH_PLAYBACK)
  371. rtac_adm_data_v2.device[i].topology_id =
  372. get_adm_rx_topology();
  373. else
  374. rtac_adm_data_v2.device[i].topology_id =
  375. get_adm_tx_topology();
  376. rtac_adm_data_v2.device[i].afe_port = port_id;
  377. rtac_adm_data_v2.device[i].copp = copp_id;
  378. rtac_adm_data_v2.device[i].popp[
  379. rtac_adm_data_v2.device[i].num_of_popp].popp = popp_id;
  380. rtac_adm_data_v2.device[i].popp[
  381. rtac_adm_data_v2.device[i].num_of_popp++].popp_topology =
  382. get_asm_topology();
  383. done:
  384. return;
  385. }
  386. static void shift_adm_devices_v2(u32 dev_idx)
  387. {
  388. for (; dev_idx < rtac_adm_data_v2.num_of_dev; dev_idx++) {
  389. memcpy(&rtac_adm_data_v2.device[dev_idx],
  390. &rtac_adm_data_v2.device[dev_idx + 1],
  391. sizeof(rtac_adm_data_v2.device[dev_idx]));
  392. memset(&rtac_adm_data_v2.device[dev_idx + 1], 0,
  393. sizeof(rtac_adm_data_v2.device[dev_idx]));
  394. }
  395. }
  396. static void shift_popp_v2(u32 copp_idx, u32 popp_idx)
  397. {
  398. for (; popp_idx < rtac_adm_data_v2.device[copp_idx].num_of_popp;
  399. popp_idx++) {
  400. memcpy(&rtac_adm_data_v2.device[copp_idx].popp[popp_idx].popp,
  401. &rtac_adm_data_v2.device[copp_idx].popp[popp_idx + 1].
  402. popp, sizeof(uint32_t));
  403. memcpy(&rtac_adm_data_v2.device[copp_idx].popp[popp_idx].
  404. popp_topology,
  405. &rtac_adm_data_v2.device[copp_idx].popp[popp_idx + 1].
  406. popp_topology,
  407. sizeof(uint32_t));
  408. memset(&rtac_adm_data_v2.device[copp_idx].popp[popp_idx + 1].
  409. popp, 0, sizeof(uint32_t));
  410. memset(&rtac_adm_data_v2.device[copp_idx].popp[popp_idx + 1].
  411. popp_topology, 0, sizeof(uint32_t));
  412. }
  413. }
  414. static void rtac_remove_adm_device_v2(u32 port_id)
  415. {
  416. s32 i;
  417. pr_debug("%s: port_id = %d\n", __func__, port_id);
  418. /* look for device */
  419. for (i = 0; i < rtac_adm_data_v2.num_of_dev; i++) {
  420. if (rtac_adm_data_v2.device[i].afe_port == port_id && rtac_adm_data_v2.device[i].num_of_popp == 0) {
  421. memset(&rtac_adm_data_v2.device[i], 0,
  422. sizeof(rtac_adm_data_v2.device[i]));
  423. rtac_adm_data_v2.num_of_dev--;
  424. if (rtac_adm_data_v2.num_of_dev >= 1) {
  425. shift_adm_devices_v2(i);
  426. break;
  427. }
  428. }
  429. }
  430. return;
  431. }
  432. static void rtac_remove_popp_from_adm_devices_v2(u32 popp_id)
  433. {
  434. s32 i, j;
  435. pr_debug("%s: popp_id = %d\n", __func__, popp_id);
  436. for (i = 0; i < rtac_adm_data_v2.num_of_dev; i++) {
  437. for (j = 0; j < rtac_adm_data_v2.device[i].num_of_popp; j++) {
  438. if (rtac_adm_data_v2.device[i].popp[j].popp ==
  439. popp_id) {
  440. rtac_adm_data_v2.device[i].popp[j].popp = 0;
  441. rtac_adm_data_v2.device[i].popp[j].
  442. popp_topology = 0;
  443. rtac_adm_data_v2.device[i].num_of_popp--;
  444. shift_popp_v2(i, j);
  445. }
  446. }
  447. }
  448. }
  449. /* ADM Info */
  450. void add_popp(u32 dev_idx, u32 port_id, u32 popp_id)
  451. {
  452. u32 i = 0;
  453. for (; i < rtac_adm_data.device[dev_idx].num_of_popp; i++)
  454. if (rtac_adm_data.device[dev_idx].popp[i] == popp_id)
  455. goto done;
  456. if (rtac_adm_data.device[dev_idx].num_of_popp ==
  457. RTAC_MAX_ACTIVE_POPP) {
  458. pr_err("%s, Max POPP!\n", __func__);
  459. goto done;
  460. }
  461. rtac_adm_data.device[dev_idx].popp[
  462. rtac_adm_data.device[dev_idx].num_of_popp++] = popp_id;
  463. done:
  464. return;
  465. }
  466. void rtac_add_adm_device(u32 port_id, u32 copp_id, u32 path_id, u32 popp_id)
  467. {
  468. u32 i = 0;
  469. pr_debug("%s: port_id = %d, popp_id = %d\n", __func__, port_id,
  470. popp_id);
  471. mutex_lock(&rtac_adm_mutex);
  472. rtac_add_adm_device_v2(port_id, copp_id, path_id, popp_id);
  473. if (rtac_adm_data.num_of_dev == RTAC_MAX_ACTIVE_DEVICES) {
  474. pr_err("%s, Can't add anymore RTAC devices!\n", __func__);
  475. goto done;
  476. }
  477. /* Check if device already added */
  478. if (rtac_adm_data.num_of_dev != 0) {
  479. for (; i < rtac_adm_data.num_of_dev; i++) {
  480. if (rtac_adm_data.device[i].afe_port == port_id &&
  481. rtac_adm_data.device[i].copp == copp_id) {
  482. add_popp(i, port_id, popp_id);
  483. goto done;
  484. }
  485. if (rtac_adm_data.device[i].num_of_popp ==
  486. RTAC_MAX_ACTIVE_POPP) {
  487. pr_err("%s, Max POPP!\n", __func__);
  488. goto done;
  489. }
  490. }
  491. }
  492. /* Add device */
  493. rtac_adm_data.num_of_dev++;
  494. if (path_id == ADM_PATH_PLAYBACK)
  495. rtac_adm_data.device[i].topology_id =
  496. get_adm_rx_topology();
  497. else
  498. rtac_adm_data.device[i].topology_id =
  499. get_adm_tx_topology();
  500. rtac_adm_data.device[i].afe_port = port_id;
  501. rtac_adm_data.device[i].copp = copp_id;
  502. rtac_adm_data.device[i].popp[
  503. rtac_adm_data.device[i].num_of_popp++] = popp_id;
  504. done:
  505. mutex_unlock(&rtac_adm_mutex);
  506. return;
  507. }
  508. static void shift_adm_devices(u32 dev_idx)
  509. {
  510. for (; dev_idx < rtac_adm_data.num_of_dev; dev_idx++) {
  511. memcpy(&rtac_adm_data.device[dev_idx],
  512. &rtac_adm_data.device[dev_idx + 1],
  513. sizeof(rtac_adm_data.device[dev_idx]));
  514. memset(&rtac_adm_data.device[dev_idx + 1], 0,
  515. sizeof(rtac_adm_data.device[dev_idx]));
  516. }
  517. }
  518. static void shift_popp(u32 copp_idx, u32 popp_idx)
  519. {
  520. for (; popp_idx < rtac_adm_data.device[copp_idx].num_of_popp;
  521. popp_idx++) {
  522. memcpy(&rtac_adm_data.device[copp_idx].popp[popp_idx],
  523. &rtac_adm_data.device[copp_idx].popp[popp_idx + 1],
  524. sizeof(uint32_t));
  525. memset(&rtac_adm_data.device[copp_idx].popp[popp_idx + 1], 0,
  526. sizeof(uint32_t));
  527. }
  528. }
  529. void rtac_remove_adm_device(u32 port_id, u32 copp_id)
  530. {
  531. s32 i;
  532. pr_debug("%s: port_id = %d\n", __func__, port_id);
  533. mutex_lock(&rtac_adm_mutex);
  534. rtac_remove_adm_device_v2(port_id);
  535. /* look for device */
  536. for (i = 0; i < rtac_adm_data.num_of_dev; i++) {
  537. if (rtac_adm_data.device[i].afe_port == port_id &&
  538. rtac_adm_data.device[i].copp == copp_id) {
  539. memset(&rtac_adm_data.device[i], 0,
  540. sizeof(rtac_adm_data.device[i]));
  541. rtac_adm_data.num_of_dev--;
  542. if (rtac_adm_data.num_of_dev >= 1) {
  543. shift_adm_devices(i);
  544. break;
  545. }
  546. }
  547. }
  548. mutex_unlock(&rtac_adm_mutex);
  549. return;
  550. }
  551. void rtac_remove_popp_from_adm_devices(u32 popp_id)
  552. {
  553. s32 i, j;
  554. pr_debug("%s: popp_id = %d\n", __func__, popp_id);
  555. mutex_lock(&rtac_adm_mutex);
  556. rtac_remove_popp_from_adm_devices_v2(popp_id);
  557. for (i = 0; i < rtac_adm_data.num_of_dev; i++) {
  558. for (j = 0; j < rtac_adm_data.device[i].num_of_popp; j++) {
  559. if (rtac_adm_data.device[i].popp[j] == popp_id) {
  560. rtac_adm_data.device[i].popp[j] = 0;
  561. rtac_adm_data.device[i].num_of_popp--;
  562. shift_popp(i, j);
  563. }
  564. }
  565. }
  566. mutex_unlock(&rtac_adm_mutex);
  567. }
  568. /* Voice Info */
  569. static void set_rtac_voice_data(int idx, u32 cvs_handle, u32 cvp_handle,
  570. u32 rx_afe_port, u32 tx_afe_port,
  571. u32 session_id)
  572. {
  573. rtac_voice_data.voice[idx].tx_topology_id = get_voice_tx_topology();
  574. rtac_voice_data.voice[idx].rx_topology_id = get_voice_rx_topology();
  575. rtac_voice_data.voice[idx].tx_afe_port = tx_afe_port;
  576. rtac_voice_data.voice[idx].rx_afe_port = rx_afe_port;
  577. rtac_voice_data.voice[idx].cvs_handle = cvs_handle;
  578. rtac_voice_data.voice[idx].cvp_handle = cvp_handle;
  579. /* Store session ID for voice RTAC */
  580. voice_session_id[idx] = session_id;
  581. }
  582. void rtac_add_voice(u32 cvs_handle, u32 cvp_handle, u32 rx_afe_port,
  583. u32 tx_afe_port, u32 session_id)
  584. {
  585. u32 i = 0;
  586. pr_debug("%s\n", __func__);
  587. mutex_lock(&rtac_voice_mutex);
  588. if (rtac_voice_data.num_of_voice_combos ==
  589. RTAC_MAX_ACTIVE_VOICE_COMBOS) {
  590. pr_err("%s, Can't add anymore RTAC devices!\n", __func__);
  591. goto done;
  592. }
  593. /* Check if device already added */
  594. if (rtac_voice_data.num_of_voice_combos != 0) {
  595. for (; i < rtac_voice_data.num_of_voice_combos; i++) {
  596. if (rtac_voice_data.voice[i].cvs_handle ==
  597. cvs_handle) {
  598. set_rtac_voice_data(i, cvs_handle, cvp_handle,
  599. rx_afe_port, tx_afe_port,
  600. session_id);
  601. goto done;
  602. }
  603. }
  604. }
  605. /* Add device */
  606. rtac_voice_data.num_of_voice_combos++;
  607. set_rtac_voice_data(i, cvs_handle, cvp_handle,
  608. rx_afe_port, tx_afe_port,
  609. session_id);
  610. done:
  611. mutex_unlock(&rtac_voice_mutex);
  612. return;
  613. }
  614. static void shift_voice_devices(u32 idx)
  615. {
  616. for (; idx < rtac_voice_data.num_of_voice_combos - 1; idx++) {
  617. memcpy(&rtac_voice_data.voice[idx],
  618. &rtac_voice_data.voice[idx + 1],
  619. sizeof(rtac_voice_data.voice[idx]));
  620. voice_session_id[idx] = voice_session_id[idx + 1];
  621. }
  622. }
  623. void rtac_remove_voice(u32 cvs_handle)
  624. {
  625. u32 i = 0;
  626. pr_debug("%s\n", __func__);
  627. mutex_lock(&rtac_voice_mutex);
  628. /* look for device */
  629. for (i = 0; i < rtac_voice_data.num_of_voice_combos; i++) {
  630. if (rtac_voice_data.voice[i].cvs_handle == cvs_handle) {
  631. shift_voice_devices(i);
  632. rtac_voice_data.num_of_voice_combos--;
  633. memset(&rtac_voice_data.voice[
  634. rtac_voice_data.num_of_voice_combos], 0,
  635. sizeof(rtac_voice_data.voice
  636. [rtac_voice_data.num_of_voice_combos]));
  637. voice_session_id[rtac_voice_data.num_of_voice_combos]
  638. = 0;
  639. break;
  640. }
  641. }
  642. mutex_unlock(&rtac_voice_mutex);
  643. return;
  644. }
  645. static u32 get_voice_session_id_cvs(u32 cvs_handle)
  646. {
  647. u32 i;
  648. for (i = 0; i < rtac_voice_data.num_of_voice_combos; i++) {
  649. if (rtac_voice_data.voice[i].cvs_handle == cvs_handle)
  650. return voice_session_id[i];
  651. }
  652. pr_err("%s: No voice index for CVS handle %d found returning 0\n",
  653. __func__, cvs_handle);
  654. return 0;
  655. }
  656. static u32 get_voice_session_id_cvp(u32 cvp_handle)
  657. {
  658. u32 i;
  659. for (i = 0; i < rtac_voice_data.num_of_voice_combos; i++) {
  660. if (rtac_voice_data.voice[i].cvp_handle == cvp_handle)
  661. return voice_session_id[i];
  662. }
  663. pr_err("%s: No voice index for CVP handle %d found returning 0\n",
  664. __func__, cvp_handle);
  665. return 0;
  666. }
  667. static int get_voice_index(u32 mode, u32 handle)
  668. {
  669. if (mode == RTAC_CVP)
  670. return voice_get_idx_for_session(
  671. get_voice_session_id_cvp(handle));
  672. if (mode == RTAC_CVS)
  673. return voice_get_idx_for_session(
  674. get_voice_session_id_cvs(handle));
  675. pr_err("%s: Invalid mode %d, returning 0\n",
  676. __func__, mode);
  677. return 0;
  678. }
  679. /* ADM APR */
  680. void rtac_set_adm_handle(void *handle)
  681. {
  682. pr_debug("%s: handle = %d\n", __func__, (unsigned int)handle);
  683. mutex_lock(&rtac_adm_apr_mutex);
  684. rtac_adm_apr_data.apr_handle = handle;
  685. mutex_unlock(&rtac_adm_apr_mutex);
  686. }
  687. bool rtac_make_adm_callback(uint32_t *payload, u32 payload_size)
  688. {
  689. pr_debug("%s:cmd_state = %d\n", __func__,
  690. atomic_read(&rtac_adm_apr_data.cmd_state));
  691. if (atomic_read(&rtac_adm_apr_data.cmd_state) != 1)
  692. return false;
  693. pr_debug("%s\n", __func__);
  694. if (payload_size == sizeof(uint32_t))
  695. atomic_set(&rtac_common.apr_err_code, payload[0]);
  696. else if (payload_size == (2*sizeof(uint32_t)))
  697. atomic_set(&rtac_common.apr_err_code, payload[1]);
  698. atomic_set(&rtac_adm_apr_data.cmd_state, 0);
  699. wake_up(&rtac_adm_apr_data.cmd_wait);
  700. return true;
  701. }
  702. u32 send_adm_apr(void *buf, u32 opcode)
  703. {
  704. s32 result;
  705. u32 user_buf_size = 0;
  706. u32 bytes_returned = 0;
  707. u32 port_index = 0;
  708. u32 copp_id;
  709. int port_id;
  710. u32 payload_size;
  711. u32 data_size = 0;
  712. struct apr_hdr adm_params;
  713. pr_debug("%s\n", __func__);
  714. if (rtac_cal[ADM_RTAC_CAL].map_data.ion_handle == NULL) {
  715. result = rtac_allocate_cal_buffer(ADM_RTAC_CAL);
  716. if (result < 0) {
  717. pr_err("%s: allocate buffer failed!",
  718. __func__);
  719. goto done;
  720. }
  721. }
  722. if (rtac_cal[ADM_RTAC_CAL].map_data.map_handle == 0) {
  723. result = rtac_map_cal_buffer(ADM_RTAC_CAL);
  724. if (result < 0) {
  725. pr_err("%s: map buffer failed!",
  726. __func__);
  727. goto done;
  728. }
  729. }
  730. if (copy_from_user(&user_buf_size, (void *)buf,
  731. sizeof(user_buf_size))) {
  732. pr_err("%s: Copy from user failed! buf = 0x%x\n",
  733. __func__, (unsigned int)buf);
  734. goto done;
  735. }
  736. if (user_buf_size <= 0) {
  737. pr_err("%s: Invalid buffer size = %d\n",
  738. __func__, user_buf_size);
  739. goto done;
  740. }
  741. if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
  742. pr_err("%s: Could not copy payload size from user buffer\n",
  743. __func__);
  744. goto done;
  745. }
  746. if (copy_from_user(&copp_id, buf + 2 * sizeof(u32), sizeof(u32))) {
  747. pr_err("%s: Could not copy port id from user buffer\n",
  748. __func__);
  749. goto done;
  750. }
  751. for (port_index = 0; port_index < AFE_MAX_PORTS; port_index++) {
  752. if (adm_get_copp_id(port_index) == copp_id)
  753. break;
  754. if (adm_get_lowlatency_copp_id(port_index) == copp_id)
  755. break;
  756. }
  757. if (port_index >= AFE_MAX_PORTS) {
  758. pr_err("%s: Could not find port index for copp = %d\n",
  759. __func__, copp_id);
  760. goto done;
  761. }
  762. port_id = q6audio_get_port_id_from_index(port_index);
  763. if (port_id < 0) {
  764. pr_err("%s: Could not find port id mapped for port_idx %d\n",
  765. __func__, port_index);
  766. goto done;
  767. }
  768. mutex_lock(&rtac_adm_apr_mutex);
  769. if (rtac_adm_apr_data.apr_handle == NULL) {
  770. pr_err("%s: APR not initialized\n", __func__);
  771. goto err;
  772. }
  773. if (opcode == ADM_CMD_SET_PP_PARAMS_V5) {
  774. /* set payload size to in-band payload */
  775. /* set data size to actual out of band payload size */
  776. data_size = payload_size - 4 * sizeof(u32);
  777. if (data_size > rtac_cal[ADM_RTAC_CAL].map_data.map_size) {
  778. pr_err("%s: Invalid data size = %d\n",
  779. __func__, data_size);
  780. goto done;
  781. }
  782. payload_size = 4 * sizeof(u32);
  783. /* Copy buffer to out-of-band payload */
  784. if (copy_from_user((void *)
  785. rtac_cal[ADM_RTAC_CAL].cal_data.kvaddr,
  786. buf + 7 * sizeof(u32), data_size)) {
  787. pr_err("%s: Could not copy payload from user buffer\n",
  788. __func__);
  789. goto err;
  790. }
  791. /* set payload size in packet */
  792. rtac_adm_buffer[8] = data_size;
  793. } else {
  794. if (payload_size > MAX_PAYLOAD_SIZE) {
  795. pr_err("%s: Invalid payload size = %d\n",
  796. __func__, payload_size);
  797. goto done;
  798. }
  799. /* Copy buffer to in-band payload */
  800. if (copy_from_user(rtac_adm_buffer +
  801. sizeof(adm_params)/sizeof(u32),
  802. buf + 3 * sizeof(u32), payload_size)) {
  803. pr_err("%s: Could not copy payload from user buffer\n",
  804. __func__);
  805. goto err;
  806. }
  807. }
  808. /* Pack header */
  809. adm_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  810. APR_HDR_LEN(20), APR_PKT_VER);
  811. adm_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  812. payload_size);
  813. adm_params.src_svc = APR_SVC_ADM;
  814. adm_params.src_domain = APR_DOMAIN_APPS;
  815. adm_params.src_port = copp_id;
  816. adm_params.dest_svc = APR_SVC_ADM;
  817. adm_params.dest_domain = APR_DOMAIN_ADSP;
  818. adm_params.dest_port = copp_id;
  819. adm_params.token = port_id;
  820. adm_params.opcode = opcode;
  821. /* fill for out-of-band */
  822. rtac_adm_buffer[5] = rtac_cal[ADM_RTAC_CAL].cal_data.paddr;
  823. rtac_adm_buffer[6] = 0;
  824. rtac_adm_buffer[7] = rtac_cal[ADM_RTAC_CAL].map_data.map_handle;
  825. memcpy(rtac_adm_buffer, &adm_params, sizeof(adm_params));
  826. atomic_set(&rtac_adm_apr_data.cmd_state, 1);
  827. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%x\n",
  828. __func__, opcode,
  829. rtac_cal[ADM_RTAC_CAL].cal_data.paddr);
  830. result = apr_send_pkt(rtac_adm_apr_data.apr_handle,
  831. (uint32_t *)rtac_adm_buffer);
  832. if (result < 0) {
  833. pr_err("%s: Set params failed port = %d, copp = %d\n",
  834. __func__, port_index, copp_id);
  835. goto err;
  836. }
  837. /* Wait for the callback */
  838. result = wait_event_timeout(rtac_adm_apr_data.cmd_wait,
  839. (atomic_read(&rtac_adm_apr_data.cmd_state) == 0),
  840. msecs_to_jiffies(TIMEOUT_MS));
  841. if (!result) {
  842. pr_err("%s: Set params timed out copp = %d\n", __func__,
  843. copp_id);
  844. goto err;
  845. }
  846. if (atomic_read(&rtac_common.apr_err_code)) {
  847. pr_err("%s: DSP returned error code = %d, opcode = 0x%x\n",
  848. __func__, atomic_read(&rtac_common.apr_err_code),
  849. opcode);
  850. goto err;
  851. }
  852. if (opcode == ADM_CMD_GET_PP_PARAMS_V5) {
  853. bytes_returned = ((u32 *)rtac_cal[ADM_RTAC_CAL].cal_data.
  854. kvaddr)[2] + 3 * sizeof(u32);
  855. if (bytes_returned > rtac_cal[ADM_RTAC_CAL].
  856. map_data.map_size) {
  857. pr_err("%s: Invalid data size = %d\n",
  858. __func__, bytes_returned);
  859. result = -EINVAL;
  860. goto err;
  861. }
  862. if (bytes_returned > user_buf_size) {
  863. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  864. __func__, user_buf_size, bytes_returned);
  865. goto err;
  866. }
  867. if (copy_to_user(buf, (void *)
  868. rtac_cal[ADM_RTAC_CAL].cal_data.kvaddr,
  869. bytes_returned)) {
  870. pr_err("%s: Could not copy buffer to user,size = %d\n",
  871. __func__, bytes_returned);
  872. goto err;
  873. }
  874. } else {
  875. bytes_returned = data_size;
  876. }
  877. err:
  878. mutex_unlock(&rtac_adm_apr_mutex);
  879. done:
  880. return bytes_returned;
  881. }
  882. /* ASM APR */
  883. void rtac_set_asm_handle(u32 session_id, void *handle)
  884. {
  885. pr_debug("%s\n", __func__);
  886. mutex_lock(&rtac_asm_apr_mutex);
  887. rtac_asm_apr_data[session_id].apr_handle = handle;
  888. mutex_unlock(&rtac_asm_apr_mutex);
  889. }
  890. bool rtac_make_asm_callback(u32 session_id, uint32_t *payload,
  891. u32 payload_size)
  892. {
  893. if (atomic_read(&rtac_asm_apr_data[session_id].cmd_state) != 1)
  894. return false;
  895. pr_debug("%s\n", __func__);
  896. if (payload_size == sizeof(uint32_t))
  897. atomic_set(&rtac_common.apr_err_code, payload[0]);
  898. else if (payload_size == (2*sizeof(uint32_t)))
  899. atomic_set(&rtac_common.apr_err_code, payload[1]);
  900. atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 0);
  901. wake_up(&rtac_asm_apr_data[session_id].cmd_wait);
  902. return true;
  903. }
  904. u32 send_rtac_asm_apr(void *buf, u32 opcode)
  905. {
  906. s32 result;
  907. u32 user_buf_size = 0;
  908. u32 bytes_returned = 0;
  909. u32 session_id = 0;
  910. u32 payload_size;
  911. u32 data_size = 0;
  912. struct apr_hdr asm_params;
  913. pr_debug("%s\n", __func__);
  914. if (rtac_cal[ASM_RTAC_CAL].map_data.ion_handle == NULL) {
  915. result = rtac_allocate_cal_buffer(ASM_RTAC_CAL);
  916. if (result < 0) {
  917. pr_err("%s: allocate buffer failed!",
  918. __func__);
  919. goto done;
  920. }
  921. }
  922. if (rtac_cal[ASM_RTAC_CAL].map_data.map_handle == 0) {
  923. result = rtac_map_cal_buffer(ASM_RTAC_CAL);
  924. if (result < 0) {
  925. pr_err("%s: map buffer failed!",
  926. __func__);
  927. goto done;
  928. }
  929. }
  930. if (copy_from_user(&user_buf_size, (void *)buf,
  931. sizeof(user_buf_size))) {
  932. pr_err("%s: Copy from user failed! buf = 0x%x\n",
  933. __func__, (unsigned int)buf);
  934. goto done;
  935. }
  936. if (user_buf_size <= 0) {
  937. pr_err("%s: Invalid buffer size = %d\n",
  938. __func__, user_buf_size);
  939. goto done;
  940. }
  941. if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
  942. pr_err("%s: Could not copy payload size from user buffer\n",
  943. __func__);
  944. goto done;
  945. }
  946. if (copy_from_user(&session_id, buf + 2 * sizeof(u32), sizeof(u32))) {
  947. pr_err("%s: Could not copy session id from user buffer\n",
  948. __func__);
  949. goto done;
  950. }
  951. if (session_id >= (SESSION_MAX + 1)) {
  952. pr_err("%s: Invalid Session = %d\n", __func__, session_id);
  953. goto done;
  954. }
  955. mutex_lock(&rtac_asm_apr_mutex);
  956. if (rtac_asm_apr_data[session_id].apr_handle == NULL) {
  957. pr_err("%s: APR not initialized\n", __func__);
  958. goto err;
  959. }
  960. if (opcode == ASM_STREAM_CMD_SET_PP_PARAMS_V2) {
  961. /* set payload size to in-band payload */
  962. /* set data size to actual out of band payload size */
  963. data_size = payload_size - 4 * sizeof(u32);
  964. if (data_size > rtac_cal[ASM_RTAC_CAL].map_data.map_size) {
  965. pr_err("%s: Invalid data size = %d\n",
  966. __func__, data_size);
  967. goto done;
  968. }
  969. payload_size = 4 * sizeof(u32);
  970. /* Copy buffer to out-of-band payload */
  971. if (copy_from_user((void *)
  972. rtac_cal[ASM_RTAC_CAL].cal_data.kvaddr,
  973. buf + 7 * sizeof(u32), data_size)) {
  974. pr_err("%s: Could not copy payload from user buffer\n",
  975. __func__);
  976. goto err;
  977. }
  978. /* set payload size in packet */
  979. rtac_asm_buffer[8] = data_size;
  980. } else {
  981. if (payload_size > MAX_PAYLOAD_SIZE) {
  982. pr_err("%s: Invalid payload size = %d\n",
  983. __func__, payload_size);
  984. goto done;
  985. }
  986. /* Copy buffer to in-band payload */
  987. if (copy_from_user(rtac_asm_buffer +
  988. sizeof(asm_params)/sizeof(u32),
  989. buf + 3 * sizeof(u32), payload_size)) {
  990. pr_err("%s: Could not copy payload from user buffer\n",
  991. __func__);
  992. goto err;
  993. }
  994. }
  995. /* Pack header */
  996. asm_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  997. APR_HDR_LEN(20), APR_PKT_VER);
  998. asm_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  999. payload_size);
  1000. asm_params.src_svc = q6asm_get_apr_service_id(session_id);
  1001. asm_params.src_domain = APR_DOMAIN_APPS;
  1002. asm_params.src_port = (session_id << 8) | 0x0001;
  1003. asm_params.dest_svc = APR_SVC_ASM;
  1004. asm_params.dest_domain = APR_DOMAIN_ADSP;
  1005. asm_params.dest_port = (session_id << 8) | 0x0001;
  1006. asm_params.token = session_id;
  1007. asm_params.opcode = opcode;
  1008. /* fill for out-of-band */
  1009. rtac_asm_buffer[5] = rtac_cal[ASM_RTAC_CAL].cal_data.paddr;
  1010. rtac_asm_buffer[6] = 0;
  1011. rtac_asm_buffer[7] = rtac_cal[ASM_RTAC_CAL].map_data.map_handle;
  1012. memcpy(rtac_asm_buffer, &asm_params, sizeof(asm_params));
  1013. atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 1);
  1014. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%x\n",
  1015. __func__, opcode,
  1016. rtac_cal[ASM_RTAC_CAL].cal_data.paddr);
  1017. result = apr_send_pkt(rtac_asm_apr_data[session_id].apr_handle,
  1018. (uint32_t *)rtac_asm_buffer);
  1019. if (result < 0) {
  1020. pr_err("%s: Set params failed session = %d\n",
  1021. __func__, session_id);
  1022. goto err;
  1023. }
  1024. /* Wait for the callback */
  1025. result = wait_event_timeout(rtac_asm_apr_data[session_id].cmd_wait,
  1026. (atomic_read(&rtac_asm_apr_data[session_id].cmd_state) == 0),
  1027. 5 * HZ);
  1028. if (!result) {
  1029. pr_err("%s: Set params timed out session = %d\n",
  1030. __func__, session_id);
  1031. goto err;
  1032. }
  1033. if (atomic_read(&rtac_common.apr_err_code)) {
  1034. pr_err("%s: DSP returned error code = %d, opcode = 0x%x\n",
  1035. __func__, atomic_read(&rtac_common.apr_err_code),
  1036. opcode);
  1037. goto err;
  1038. }
  1039. if (opcode == ASM_STREAM_CMD_GET_PP_PARAMS_V2) {
  1040. bytes_returned = ((u32 *)rtac_cal[ASM_RTAC_CAL].cal_data.
  1041. kvaddr)[2] + 3 * sizeof(u32);
  1042. if (bytes_returned > rtac_cal[ASM_RTAC_CAL].
  1043. map_data.map_size) {
  1044. pr_err("%s: Invalid data size = %d\n",
  1045. __func__, bytes_returned);
  1046. result = -EINVAL;
  1047. goto err;
  1048. }
  1049. if (bytes_returned > user_buf_size) {
  1050. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  1051. __func__, user_buf_size, bytes_returned);
  1052. goto err;
  1053. }
  1054. if (copy_to_user(buf, (void *)
  1055. rtac_cal[ASM_RTAC_CAL].cal_data.kvaddr,
  1056. bytes_returned)) {
  1057. pr_err("%s: Could not copy buffer to user,size = %d\n",
  1058. __func__, bytes_returned);
  1059. goto err;
  1060. }
  1061. } else {
  1062. bytes_returned = data_size;
  1063. }
  1064. err:
  1065. mutex_unlock(&rtac_asm_apr_mutex);
  1066. done:
  1067. return bytes_returned;
  1068. }
  1069. /* Voice APR */
  1070. void rtac_set_voice_handle(u32 mode, void *handle)
  1071. {
  1072. pr_debug("%s\n", __func__);
  1073. mutex_lock(&rtac_voice_apr_mutex);
  1074. rtac_voice_apr_data[mode].apr_handle = handle;
  1075. mutex_unlock(&rtac_voice_apr_mutex);
  1076. }
  1077. bool rtac_make_voice_callback(u32 mode, uint32_t *payload, u32 payload_size)
  1078. {
  1079. if ((atomic_read(&rtac_voice_apr_data[mode].cmd_state) != 1) ||
  1080. (mode >= RTAC_VOICE_MODES))
  1081. return false;
  1082. pr_debug("%s\n", __func__);
  1083. if (payload_size == sizeof(uint32_t))
  1084. atomic_set(&rtac_common.apr_err_code, payload[0]);
  1085. else if (payload_size == (2*sizeof(uint32_t)))
  1086. atomic_set(&rtac_common.apr_err_code, payload[1]);
  1087. atomic_set(&rtac_voice_apr_data[mode].cmd_state, 0);
  1088. wake_up(&rtac_voice_apr_data[mode].cmd_wait);
  1089. return true;
  1090. }
  1091. u32 send_voice_apr(u32 mode, void *buf, u32 opcode)
  1092. {
  1093. s32 result;
  1094. u32 user_buf_size = 0;
  1095. u32 bytes_returned = 0;
  1096. u32 payload_size;
  1097. u32 dest_port;
  1098. u32 data_size = 0;
  1099. struct apr_hdr voice_params;
  1100. pr_debug("%s\n", __func__);
  1101. if (rtac_cal[VOICE_RTAC_CAL].map_data.ion_handle == NULL) {
  1102. result = rtac_allocate_cal_buffer(VOICE_RTAC_CAL);
  1103. if (result < 0) {
  1104. pr_err("%s: allocate buffer failed!",
  1105. __func__);
  1106. goto done;
  1107. }
  1108. }
  1109. if (rtac_cal[VOICE_RTAC_CAL].map_data.map_handle == 0) {
  1110. result = rtac_map_cal_buffer(VOICE_RTAC_CAL);
  1111. if (result < 0) {
  1112. pr_err("%s: map buffer failed!",
  1113. __func__);
  1114. goto done;
  1115. }
  1116. }
  1117. if (copy_from_user(&user_buf_size, (void *)buf,
  1118. sizeof(user_buf_size))) {
  1119. pr_err("%s: Copy from user failed! buf = 0x%x\n",
  1120. __func__, (unsigned int)buf);
  1121. goto done;
  1122. }
  1123. if (user_buf_size <= 0) {
  1124. pr_err("%s: Invalid buffer size = %d\n",
  1125. __func__, user_buf_size);
  1126. goto done;
  1127. }
  1128. if (copy_from_user(&payload_size, buf + sizeof(u32), sizeof(u32))) {
  1129. pr_err("%s: Could not copy payload size from user buffer\n",
  1130. __func__);
  1131. goto done;
  1132. }
  1133. if (copy_from_user(&dest_port, buf + 2 * sizeof(u32), sizeof(u32))) {
  1134. pr_err("%s: Could not copy port id from user buffer\n",
  1135. __func__);
  1136. goto done;
  1137. }
  1138. if ((mode != RTAC_CVP) && (mode != RTAC_CVS)) {
  1139. pr_err("%s: Invalid Mode for APR, mode = %d\n",
  1140. __func__, mode);
  1141. goto done;
  1142. }
  1143. mutex_lock(&rtac_voice_apr_mutex);
  1144. if (rtac_voice_apr_data[mode].apr_handle == NULL) {
  1145. pr_err("%s: APR not initialized\n", __func__);
  1146. goto err;
  1147. }
  1148. if (opcode == VOICE_CMD_SET_PARAM) {
  1149. /* set payload size to in-band payload */
  1150. /* set data size to actual out of band payload size */
  1151. data_size = payload_size - 4 * sizeof(u32);
  1152. if (data_size > rtac_cal[VOICE_RTAC_CAL].map_data.map_size) {
  1153. pr_err("%s: Invalid data size = %d\n",
  1154. __func__, data_size);
  1155. goto done;
  1156. }
  1157. payload_size = 4 * sizeof(u32);
  1158. /* Copy buffer to out-of-band payload */
  1159. if (copy_from_user((void *)
  1160. rtac_cal[VOICE_RTAC_CAL].cal_data.kvaddr,
  1161. buf + 7 * sizeof(u32), data_size)) {
  1162. pr_err("%s: Could not copy payload from user buffer\n",
  1163. __func__);
  1164. goto err;
  1165. }
  1166. /* set payload size in packet */
  1167. rtac_voice_buffer[8] = data_size;
  1168. } else {
  1169. if (payload_size > MAX_PAYLOAD_SIZE) {
  1170. pr_err("%s: Invalid payload size = %d\n",
  1171. __func__, payload_size);
  1172. goto done;
  1173. }
  1174. /* Copy buffer to in-band payload */
  1175. if (copy_from_user(rtac_voice_buffer +
  1176. sizeof(voice_params)/sizeof(u32),
  1177. buf + 3 * sizeof(u32), payload_size)) {
  1178. pr_err("%s: Could not copy payload from user buffer\n",
  1179. __func__);
  1180. goto err;
  1181. }
  1182. }
  1183. /* Pack header */
  1184. voice_params.hdr_field = APR_HDR_FIELD(APR_MSG_TYPE_SEQ_CMD,
  1185. APR_HDR_LEN(20), APR_PKT_VER);
  1186. voice_params.pkt_size = APR_PKT_SIZE(APR_HDR_SIZE,
  1187. payload_size);
  1188. voice_params.src_svc = 0;
  1189. voice_params.src_domain = APR_DOMAIN_APPS;
  1190. voice_params.src_port = get_voice_index(mode, dest_port);
  1191. voice_params.dest_svc = 0;
  1192. voice_params.dest_domain = APR_DOMAIN_MODEM;
  1193. voice_params.dest_port = (u16)dest_port;
  1194. voice_params.token = 0;
  1195. voice_params.opcode = opcode;
  1196. /* fill for out-of-band */
  1197. rtac_voice_buffer[5] = rtac_cal[VOICE_RTAC_CAL].map_data.map_handle;
  1198. rtac_voice_buffer[6] = rtac_cal[VOICE_RTAC_CAL].cal_data.paddr;
  1199. rtac_voice_buffer[7] = 0;
  1200. memcpy(rtac_voice_buffer, &voice_params, sizeof(voice_params));
  1201. atomic_set(&rtac_voice_apr_data[mode].cmd_state, 1);
  1202. pr_debug("%s: Sending RTAC command ioctl 0x%x, paddr 0x%x\n",
  1203. __func__, opcode,
  1204. rtac_cal[VOICE_RTAC_CAL].cal_data.paddr);
  1205. result = apr_send_pkt(rtac_voice_apr_data[mode].apr_handle,
  1206. (uint32_t *)rtac_voice_buffer);
  1207. if (result < 0) {
  1208. pr_err("%s: apr_send_pkt failed opcode = %x\n",
  1209. __func__, opcode);
  1210. goto err;
  1211. }
  1212. /* Wait for the callback */
  1213. result = wait_event_timeout(rtac_voice_apr_data[mode].cmd_wait,
  1214. (atomic_read(&rtac_voice_apr_data[mode].cmd_state) == 0),
  1215. msecs_to_jiffies(TIMEOUT_MS));
  1216. if (!result) {
  1217. pr_err("%s: apr_send_pkt timed out opcode = %x\n",
  1218. __func__, opcode);
  1219. goto err;
  1220. }
  1221. if (atomic_read(&rtac_common.apr_err_code)) {
  1222. pr_err("%s: DSP returned error code = %d, opcode = 0x%x\n",
  1223. __func__, atomic_read(&rtac_common.apr_err_code),
  1224. opcode);
  1225. goto err;
  1226. }
  1227. if (opcode == VOICE_CMD_GET_PARAM) {
  1228. bytes_returned = ((u32 *)rtac_cal[VOICE_RTAC_CAL].cal_data.
  1229. kvaddr)[2] + 3 * sizeof(u32);
  1230. if (bytes_returned > rtac_cal[VOICE_RTAC_CAL].
  1231. map_data.map_size) {
  1232. pr_err("%s: Invalid data size = %d\n",
  1233. __func__, bytes_returned);
  1234. result = -EINVAL;
  1235. goto err;
  1236. }
  1237. if (bytes_returned > user_buf_size) {
  1238. pr_err("%s: User buf not big enough, size = 0x%x, returned size = 0x%x\n",
  1239. __func__, user_buf_size, bytes_returned);
  1240. goto err;
  1241. }
  1242. if (copy_to_user(buf, (void *)
  1243. rtac_cal[VOICE_RTAC_CAL].cal_data.kvaddr,
  1244. bytes_returned)) {
  1245. pr_err("%s: Could not copy buffer to user, size = %d\n",
  1246. __func__, bytes_returned);
  1247. goto err;
  1248. }
  1249. } else {
  1250. bytes_returned = data_size;
  1251. }
  1252. err:
  1253. mutex_unlock(&rtac_voice_apr_mutex);
  1254. done:
  1255. return bytes_returned;
  1256. }
  1257. static long rtac_ioctl(struct file *f,
  1258. unsigned int cmd, unsigned long arg)
  1259. {
  1260. s32 result = 0;
  1261. pr_debug("%s\n", __func__);
  1262. if (arg == 0) {
  1263. pr_err("%s: No data sent to driver!\n", __func__);
  1264. result = -EFAULT;
  1265. goto done;
  1266. }
  1267. switch (cmd) {
  1268. case AUDIO_GET_RTAC_ADM_INFO:
  1269. if (copy_to_user((void *)arg, &rtac_adm_data,
  1270. sizeof(rtac_adm_data)))
  1271. pr_err("%s: Could not copy to userspace!\n", __func__);
  1272. else
  1273. result = sizeof(rtac_adm_data);
  1274. break;
  1275. case AUDIO_GET_RTAC_ADM_INFO_V2:
  1276. if (copy_to_user((void *)arg, &rtac_adm_data_v2,
  1277. sizeof(rtac_adm_data_v2)))
  1278. pr_err("%s: Could not copy to userspace!\n", __func__);
  1279. else
  1280. result = sizeof(rtac_adm_data_v2);
  1281. break;
  1282. case AUDIO_GET_RTAC_VOICE_INFO:
  1283. if (copy_to_user((void *)arg, &rtac_voice_data,
  1284. sizeof(rtac_voice_data)))
  1285. pr_err("%s: Could not copy to userspace!\n", __func__);
  1286. else
  1287. result = sizeof(rtac_voice_data);
  1288. break;
  1289. case AUDIO_GET_RTAC_ADM_CAL:
  1290. result = send_adm_apr((void *)arg, ADM_CMD_GET_PP_PARAMS_V5);
  1291. break;
  1292. case AUDIO_SET_RTAC_ADM_CAL:
  1293. result = send_adm_apr((void *)arg, ADM_CMD_SET_PP_PARAMS_V5);
  1294. break;
  1295. case AUDIO_GET_RTAC_ASM_CAL:
  1296. result = send_rtac_asm_apr((void *)arg,
  1297. ASM_STREAM_CMD_GET_PP_PARAMS_V2);
  1298. break;
  1299. case AUDIO_SET_RTAC_ASM_CAL:
  1300. result = send_rtac_asm_apr((void *)arg,
  1301. ASM_STREAM_CMD_SET_PP_PARAMS_V2);
  1302. break;
  1303. case AUDIO_GET_RTAC_CVS_CAL:
  1304. result = send_voice_apr(RTAC_CVS, (void *)arg,
  1305. VOICE_CMD_GET_PARAM);
  1306. break;
  1307. case AUDIO_SET_RTAC_CVS_CAL:
  1308. result = send_voice_apr(RTAC_CVS, (void *)arg,
  1309. VOICE_CMD_SET_PARAM);
  1310. break;
  1311. case AUDIO_GET_RTAC_CVP_CAL:
  1312. result = send_voice_apr(RTAC_CVP, (void *)arg,
  1313. VOICE_CMD_GET_PARAM);
  1314. break;
  1315. case AUDIO_SET_RTAC_CVP_CAL:
  1316. result = send_voice_apr(RTAC_CVP, (void *)arg,
  1317. VOICE_CMD_SET_PARAM);
  1318. break;
  1319. default:
  1320. pr_err("%s: Invalid IOCTL, command = %d!\n",
  1321. __func__, cmd);
  1322. }
  1323. done:
  1324. return result;
  1325. }
  1326. static const struct file_operations rtac_fops = {
  1327. .owner = THIS_MODULE,
  1328. .open = rtac_open,
  1329. .release = rtac_release,
  1330. .unlocked_ioctl = rtac_ioctl,
  1331. };
  1332. struct miscdevice rtac_misc = {
  1333. .minor = MISC_DYNAMIC_MINOR,
  1334. .name = "msm_rtac",
  1335. .fops = &rtac_fops,
  1336. };
  1337. static int __init rtac_init(void)
  1338. {
  1339. int i = 0;
  1340. pr_debug("%s\n", __func__);
  1341. /* Driver */
  1342. atomic_set(&rtac_common.usage_count, 0);
  1343. atomic_set(&rtac_common.apr_err_code, 0);
  1344. /* ADM */
  1345. memset(&rtac_adm_data, 0, sizeof(rtac_adm_data));
  1346. memset(&rtac_adm_data_v2, 0, sizeof(rtac_adm_data_v2));
  1347. rtac_adm_apr_data.apr_handle = NULL;
  1348. atomic_set(&rtac_adm_apr_data.cmd_state, 0);
  1349. init_waitqueue_head(&rtac_adm_apr_data.cmd_wait);
  1350. mutex_init(&rtac_adm_mutex);
  1351. mutex_init(&rtac_adm_apr_mutex);
  1352. rtac_adm_buffer = kzalloc(
  1353. rtac_cal[ADM_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1354. if (rtac_adm_buffer == NULL) {
  1355. pr_err("%s: Could not allocate payload of size = %d\n",
  1356. __func__, rtac_cal[ADM_RTAC_CAL].map_data.map_size);
  1357. goto nomem;
  1358. }
  1359. /* ASM */
  1360. for (i = 0; i < SESSION_MAX+1; i++) {
  1361. rtac_asm_apr_data[i].apr_handle = NULL;
  1362. atomic_set(&rtac_asm_apr_data[i].cmd_state, 0);
  1363. init_waitqueue_head(&rtac_asm_apr_data[i].cmd_wait);
  1364. }
  1365. mutex_init(&rtac_asm_apr_mutex);
  1366. rtac_asm_buffer = kzalloc(
  1367. rtac_cal[ASM_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1368. if (rtac_asm_buffer == NULL) {
  1369. pr_err("%s: Could not allocate payload of size = %d\n",
  1370. __func__, rtac_cal[ASM_RTAC_CAL].map_data.map_size);
  1371. kzfree(rtac_adm_buffer);
  1372. goto nomem;
  1373. }
  1374. /* Voice */
  1375. memset(&rtac_voice_data, 0, sizeof(rtac_voice_data));
  1376. for (i = 0; i < RTAC_VOICE_MODES; i++) {
  1377. rtac_voice_apr_data[i].apr_handle = NULL;
  1378. atomic_set(&rtac_voice_apr_data[i].cmd_state, 0);
  1379. init_waitqueue_head(&rtac_voice_apr_data[i].cmd_wait);
  1380. }
  1381. mutex_init(&rtac_voice_mutex);
  1382. mutex_init(&rtac_voice_apr_mutex);
  1383. rtac_voice_buffer = kzalloc(
  1384. rtac_cal[VOICE_RTAC_CAL].map_data.map_size, GFP_KERNEL);
  1385. if (rtac_voice_buffer == NULL) {
  1386. pr_err("%s: Could not allocate payload of size = %d\n",
  1387. __func__, rtac_cal[VOICE_RTAC_CAL].map_data.map_size);
  1388. kzfree(rtac_adm_buffer);
  1389. kzfree(rtac_asm_buffer);
  1390. goto nomem;
  1391. }
  1392. return misc_register(&rtac_misc);
  1393. nomem:
  1394. return -ENOMEM;
  1395. }
  1396. module_init(rtac_init);
  1397. MODULE_DESCRIPTION("SoC QDSP6v2 Real-Time Audio Calibration driver");
  1398. MODULE_LICENSE("GPL v2");
  1399. #endif