rtac.c 51 KB

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