audio_cal_utils.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include <linux/fs.h>
  7. #include <linux/module.h>
  8. #include <linux/miscdevice.h>
  9. #include <linux/uaccess.h>
  10. #include <linux/mutex.h>
  11. #include <dsp/audio_cal_utils.h>
  12. static int unmap_memory(struct cal_type_data *cal_type,
  13. struct cal_block_data *cal_block);
  14. size_t get_cal_info_size(int32_t cal_type)
  15. {
  16. size_t size = 0;
  17. size_t size1 = 0, size2 = 0;
  18. switch (cal_type) {
  19. case CVP_VOC_RX_TOPOLOGY_CAL_TYPE:
  20. size = sizeof(struct audio_cal_info_voc_top);
  21. break;
  22. case CVP_VOC_TX_TOPOLOGY_CAL_TYPE:
  23. size = sizeof(struct audio_cal_info_voc_top);
  24. break;
  25. case CVP_VOCPROC_STATIC_CAL_TYPE:
  26. size = sizeof(struct audio_cal_info_vocproc);
  27. break;
  28. case CVP_VOCPROC_DYNAMIC_CAL_TYPE:
  29. size = sizeof(struct audio_cal_info_vocvol);
  30. break;
  31. case CVS_VOCSTRM_STATIC_CAL_TYPE:
  32. size = 0;
  33. break;
  34. case CVP_VOCDEV_CFG_CAL_TYPE:
  35. size = sizeof(struct audio_cal_info_vocdev_cfg);
  36. break;
  37. case CVP_VOCPROC_STATIC_COL_CAL_TYPE:
  38. size = sizeof(struct audio_cal_info_voc_col);
  39. break;
  40. case CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE:
  41. size = sizeof(struct audio_cal_info_voc_col);
  42. break;
  43. case CVS_VOCSTRM_STATIC_COL_CAL_TYPE:
  44. size = sizeof(struct audio_cal_info_voc_col);
  45. break;
  46. case ADM_TOPOLOGY_CAL_TYPE:
  47. case ADM_LSM_TOPOLOGY_CAL_TYPE:
  48. size = sizeof(struct audio_cal_info_adm_top);
  49. break;
  50. case ADM_CUST_TOPOLOGY_CAL_TYPE:
  51. case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
  52. size = 0;
  53. break;
  54. case ADM_AUDPROC_CAL_TYPE:
  55. case ADM_LSM_AUDPROC_CAL_TYPE:
  56. case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
  57. case ADM_AUDPROC_PERSISTENT_CAL_TYPE:
  58. size = sizeof(struct audio_cal_info_audproc);
  59. break;
  60. case ADM_AUDVOL_CAL_TYPE:
  61. case ADM_RTAC_AUDVOL_CAL_TYPE:
  62. size = sizeof(struct audio_cal_info_audvol);
  63. break;
  64. case ASM_TOPOLOGY_CAL_TYPE:
  65. size = sizeof(struct audio_cal_info_asm_top);
  66. break;
  67. case ASM_CUST_TOPOLOGY_CAL_TYPE:
  68. size = 0;
  69. break;
  70. case ASM_AUDSTRM_CAL_TYPE:
  71. size = sizeof(struct audio_cal_info_audstrm);
  72. break;
  73. case AFE_TOPOLOGY_CAL_TYPE:
  74. case AFE_LSM_TOPOLOGY_CAL_TYPE:
  75. size = sizeof(struct audio_cal_info_afe_top);
  76. break;
  77. case AFE_CUST_TOPOLOGY_CAL_TYPE:
  78. size = 0;
  79. break;
  80. case AFE_COMMON_RX_CAL_TYPE:
  81. size = sizeof(struct audio_cal_info_afe);
  82. break;
  83. case AFE_COMMON_TX_CAL_TYPE:
  84. case AFE_LSM_TX_CAL_TYPE:
  85. size = sizeof(struct audio_cal_info_afe);
  86. break;
  87. case AFE_FB_SPKR_PROT_CAL_TYPE:
  88. size = sizeof(struct audio_cal_info_spk_prot_cfg);
  89. break;
  90. case AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE:
  91. /*
  92. * Since get and set parameter structures are different in size
  93. * use the maximum size of get and set parameter structure
  94. */
  95. size1 = max(sizeof(struct audio_cal_info_sp_th_vi_ftm_cfg),
  96. sizeof(struct audio_cal_info_sp_th_vi_param));
  97. size2 = max(sizeof(struct audio_cal_info_sp_th_vi_v_vali_cfg),
  98. sizeof(struct audio_cal_info_sp_th_vi_v_vali_param));
  99. size = max(size1, size2);
  100. break;
  101. case AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE:
  102. /*
  103. * Since get and set parameter structures are different in size
  104. * use the maximum size of get and set parameter structure
  105. */
  106. size = max(sizeof(struct audio_cal_info_sp_ex_vi_ftm_cfg),
  107. sizeof(struct audio_cal_info_sp_ex_vi_param));
  108. break;
  109. case AFE_FB_SPKR_PROT_V4_EX_VI_CAL_TYPE:
  110. size = sizeof(struct audio_cal_info_sp_v4_ex_vi_param);
  111. break;
  112. case AFE_ANC_CAL_TYPE:
  113. size = 0;
  114. break;
  115. case AFE_AANC_CAL_TYPE:
  116. size = sizeof(struct audio_cal_info_aanc);
  117. break;
  118. case AFE_HW_DELAY_CAL_TYPE:
  119. size = sizeof(struct audio_cal_info_hw_delay);
  120. break;
  121. case AFE_SIDETONE_CAL_TYPE:
  122. size = sizeof(struct audio_cal_info_sidetone);
  123. break;
  124. case AFE_SIDETONE_IIR_CAL_TYPE:
  125. size = sizeof(struct audio_cal_info_sidetone_iir);
  126. break;
  127. case LSM_CUST_TOPOLOGY_CAL_TYPE:
  128. size = 0;
  129. break;
  130. case LSM_TOPOLOGY_CAL_TYPE:
  131. size = sizeof(struct audio_cal_info_lsm_top);
  132. break;
  133. case ULP_LSM_TOPOLOGY_ID_CAL_TYPE:
  134. size = sizeof(struct audio_cal_info_lsm_top);
  135. break;
  136. case LSM_CAL_TYPE:
  137. size = sizeof(struct audio_cal_info_lsm);
  138. break;
  139. case ADM_RTAC_INFO_CAL_TYPE:
  140. size = 0;
  141. break;
  142. case VOICE_RTAC_INFO_CAL_TYPE:
  143. size = 0;
  144. break;
  145. case ADM_RTAC_APR_CAL_TYPE:
  146. size = 0;
  147. break;
  148. case ASM_RTAC_APR_CAL_TYPE:
  149. size = 0;
  150. break;
  151. case VOICE_RTAC_APR_CAL_TYPE:
  152. size = 0;
  153. break;
  154. case MAD_CAL_TYPE:
  155. size = 0;
  156. break;
  157. case ULP_AFE_CAL_TYPE:
  158. size = sizeof(struct audio_cal_info_afe);
  159. break;
  160. case ULP_LSM_CAL_TYPE:
  161. size = sizeof(struct audio_cal_info_lsm);
  162. break;
  163. case AUDIO_CORE_METAINFO_CAL_TYPE:
  164. size = sizeof(struct audio_cal_info_metainfo);
  165. break;
  166. case SRS_TRUMEDIA_CAL_TYPE:
  167. size = 0;
  168. break;
  169. default:
  170. pr_err("%s:Invalid cal type %d!",
  171. __func__, cal_type);
  172. }
  173. return size;
  174. }
  175. size_t get_user_cal_type_size(int32_t cal_type)
  176. {
  177. size_t size = 0;
  178. switch (cal_type) {
  179. case CVP_VOC_RX_TOPOLOGY_CAL_TYPE:
  180. size = sizeof(struct audio_cal_type_voc_top);
  181. break;
  182. case CVP_VOC_TX_TOPOLOGY_CAL_TYPE:
  183. size = sizeof(struct audio_cal_type_voc_top);
  184. break;
  185. case CVP_VOCPROC_STATIC_CAL_TYPE:
  186. size = sizeof(struct audio_cal_type_vocproc);
  187. break;
  188. case CVP_VOCPROC_DYNAMIC_CAL_TYPE:
  189. size = sizeof(struct audio_cal_type_vocvol);
  190. break;
  191. case CVS_VOCSTRM_STATIC_CAL_TYPE:
  192. size = sizeof(struct audio_cal_type_basic);
  193. break;
  194. case CVP_VOCDEV_CFG_CAL_TYPE:
  195. size = sizeof(struct audio_cal_type_vocdev_cfg);
  196. break;
  197. case CVP_VOCPROC_STATIC_COL_CAL_TYPE:
  198. case CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE:
  199. case CVS_VOCSTRM_STATIC_COL_CAL_TYPE:
  200. size = sizeof(struct audio_cal_type_voc_col);
  201. break;
  202. case ADM_TOPOLOGY_CAL_TYPE:
  203. case ADM_LSM_TOPOLOGY_CAL_TYPE:
  204. size = sizeof(struct audio_cal_type_adm_top);
  205. break;
  206. case ADM_CUST_TOPOLOGY_CAL_TYPE:
  207. case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
  208. size = sizeof(struct audio_cal_type_basic);
  209. break;
  210. case ADM_AUDPROC_CAL_TYPE:
  211. case ADM_LSM_AUDPROC_CAL_TYPE:
  212. case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
  213. case ADM_AUDPROC_PERSISTENT_CAL_TYPE:
  214. size = sizeof(struct audio_cal_type_audproc);
  215. break;
  216. case ADM_AUDVOL_CAL_TYPE:
  217. case ADM_RTAC_AUDVOL_CAL_TYPE:
  218. size = sizeof(struct audio_cal_type_audvol);
  219. break;
  220. case ASM_TOPOLOGY_CAL_TYPE:
  221. size = sizeof(struct audio_cal_type_asm_top);
  222. break;
  223. case ASM_CUST_TOPOLOGY_CAL_TYPE:
  224. size = sizeof(struct audio_cal_type_basic);
  225. break;
  226. case ASM_AUDSTRM_CAL_TYPE:
  227. size = sizeof(struct audio_cal_type_audstrm);
  228. break;
  229. case AFE_TOPOLOGY_CAL_TYPE:
  230. case AFE_LSM_TOPOLOGY_CAL_TYPE:
  231. size = sizeof(struct audio_cal_type_afe_top);
  232. break;
  233. case AFE_CUST_TOPOLOGY_CAL_TYPE:
  234. size = sizeof(struct audio_cal_type_basic);
  235. break;
  236. case AFE_COMMON_RX_CAL_TYPE:
  237. size = sizeof(struct audio_cal_type_afe);
  238. break;
  239. case AFE_COMMON_TX_CAL_TYPE:
  240. case AFE_LSM_TX_CAL_TYPE:
  241. size = sizeof(struct audio_cal_type_afe);
  242. break;
  243. case AFE_FB_SPKR_PROT_CAL_TYPE:
  244. size = sizeof(struct audio_cal_type_fb_spk_prot_cfg);
  245. break;
  246. case AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE:
  247. /*
  248. * Since get and set parameter structures are different in size
  249. * use the maximum size of get and set parameter structure
  250. */
  251. size = max(sizeof(struct audio_cal_type_sp_th_vi_ftm_cfg),
  252. sizeof(struct audio_cal_type_sp_th_vi_param));
  253. break;
  254. case AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE:
  255. /*
  256. * Since get and set parameter structures are different in size
  257. * use the maximum size of get and set parameter structure
  258. */
  259. size = max(sizeof(struct audio_cal_type_sp_ex_vi_ftm_cfg),
  260. sizeof(struct audio_cal_type_sp_ex_vi_param));
  261. break;
  262. case AFE_FB_SPKR_PROT_V4_EX_VI_CAL_TYPE:
  263. size = sizeof(struct audio_cal_type_sp_v4_ex_vi_param);
  264. break;
  265. case AFE_ANC_CAL_TYPE:
  266. size = 0;
  267. break;
  268. case AFE_AANC_CAL_TYPE:
  269. size = sizeof(struct audio_cal_type_aanc);
  270. break;
  271. case AFE_HW_DELAY_CAL_TYPE:
  272. size = sizeof(struct audio_cal_type_hw_delay);
  273. break;
  274. case AFE_SIDETONE_CAL_TYPE:
  275. size = sizeof(struct audio_cal_type_sidetone);
  276. break;
  277. case AFE_SIDETONE_IIR_CAL_TYPE:
  278. size = sizeof(struct audio_cal_type_sidetone_iir);
  279. break;
  280. case LSM_CUST_TOPOLOGY_CAL_TYPE:
  281. size = sizeof(struct audio_cal_type_basic);
  282. break;
  283. case LSM_TOPOLOGY_CAL_TYPE:
  284. size = sizeof(struct audio_cal_type_lsm_top);
  285. break;
  286. case ULP_LSM_TOPOLOGY_ID_CAL_TYPE:
  287. size = sizeof(struct audio_cal_type_lsm_top);
  288. break;
  289. case LSM_CAL_TYPE:
  290. size = sizeof(struct audio_cal_type_lsm);
  291. break;
  292. case ADM_RTAC_INFO_CAL_TYPE:
  293. size = 0;
  294. break;
  295. case VOICE_RTAC_INFO_CAL_TYPE:
  296. size = 0;
  297. break;
  298. case ADM_RTAC_APR_CAL_TYPE:
  299. size = 0;
  300. break;
  301. case ASM_RTAC_APR_CAL_TYPE:
  302. size = 0;
  303. break;
  304. case VOICE_RTAC_APR_CAL_TYPE:
  305. size = 0;
  306. break;
  307. case MAD_CAL_TYPE:
  308. size = 0;
  309. break;
  310. case ULP_AFE_CAL_TYPE:
  311. size = sizeof(struct audio_cal_type_afe);
  312. break;
  313. case ULP_LSM_CAL_TYPE:
  314. size = sizeof(struct audio_cal_type_lsm);
  315. break;
  316. case AUDIO_CORE_METAINFO_CAL_TYPE:
  317. size = sizeof(struct audio_cal_type_metainfo);
  318. break;
  319. case SRS_TRUMEDIA_CAL_TYPE:
  320. size = 0;
  321. break;
  322. default:
  323. pr_err("%s:Invalid cal type %d!",
  324. __func__, cal_type);
  325. }
  326. return size;
  327. }
  328. int32_t cal_utils_get_cal_type_version(void *cal_type_data)
  329. {
  330. struct audio_cal_type_basic *data = NULL;
  331. data = (struct audio_cal_type_basic *)cal_type_data;
  332. return data->cal_hdr.version;
  333. }
  334. static struct cal_type_data *create_cal_type_data(
  335. struct cal_type_info *info)
  336. {
  337. struct cal_type_data *cal_type = NULL;
  338. if ((info->reg.cal_type < 0) ||
  339. (info->reg.cal_type >= MAX_CAL_TYPES)) {
  340. pr_err("%s: cal type %d is Invalid!\n",
  341. __func__, info->reg.cal_type);
  342. goto done;
  343. }
  344. if (info->cal_util_callbacks.match_block == NULL) {
  345. pr_err("%s: cal type %d no method to match blocks!\n",
  346. __func__, info->reg.cal_type);
  347. goto done;
  348. }
  349. cal_type = kmalloc(sizeof(*cal_type), GFP_KERNEL);
  350. if (cal_type == NULL)
  351. goto done;
  352. INIT_LIST_HEAD(&cal_type->cal_blocks);
  353. mutex_init(&cal_type->lock);
  354. memcpy(&cal_type->info, info,
  355. sizeof(cal_type->info));
  356. done:
  357. return cal_type;
  358. }
  359. /**
  360. * cal_utils_create_cal_types
  361. *
  362. * @num_cal_types: number of types
  363. * @cal_type: pointer to the cal types pointer
  364. * @info: pointer to info
  365. *
  366. * Returns 0 on success, EINVAL otherwise
  367. */
  368. int cal_utils_create_cal_types(int num_cal_types,
  369. struct cal_type_data **cal_type,
  370. struct cal_type_info *info)
  371. {
  372. int ret = 0;
  373. int i;
  374. pr_debug("%s\n", __func__);
  375. if (cal_type == NULL) {
  376. pr_err("%s: cal_type is NULL!\n", __func__);
  377. ret = -EINVAL;
  378. goto done;
  379. } else if (info == NULL) {
  380. pr_err("%s: info is NULL!\n", __func__);
  381. ret = -EINVAL;
  382. goto done;
  383. } else if ((num_cal_types <= 0) ||
  384. (num_cal_types > MAX_CAL_TYPES)) {
  385. pr_err("%s: num_cal_types of %d is Invalid!\n",
  386. __func__, num_cal_types);
  387. ret = -EINVAL;
  388. goto done;
  389. }
  390. for (i = 0; i < num_cal_types; i++) {
  391. if ((info[i].reg.cal_type < 0) ||
  392. (info[i].reg.cal_type >= MAX_CAL_TYPES)) {
  393. pr_err("%s: cal type %d at index %d is Invalid!\n",
  394. __func__, info[i].reg.cal_type, i);
  395. ret = -EINVAL;
  396. goto done;
  397. }
  398. cal_type[i] = create_cal_type_data(&info[i]);
  399. if (cal_type[i] == NULL) {
  400. pr_err("%s: Could not allocate cal_type of index %d!\n",
  401. __func__, i);
  402. ret = -EINVAL;
  403. goto done;
  404. }
  405. ret = audio_cal_register(1, &info[i].reg);
  406. if (ret < 0) {
  407. pr_err("%s: audio_cal_register failed, ret = %d!\n",
  408. __func__, ret);
  409. ret = -EINVAL;
  410. goto done;
  411. }
  412. pr_debug("%s: cal type %d at index %d!\n",
  413. __func__, info[i].reg.cal_type, i);
  414. }
  415. done:
  416. return ret;
  417. }
  418. EXPORT_SYMBOL(cal_utils_create_cal_types);
  419. static void delete_cal_block(struct cal_block_data *cal_block)
  420. {
  421. pr_debug("%s\n", __func__);
  422. if (cal_block == NULL)
  423. goto done;
  424. list_del(&cal_block->list);
  425. kfree(cal_block->client_info);
  426. cal_block->client_info = NULL;
  427. kfree(cal_block->cal_info);
  428. cal_block->cal_info = NULL;
  429. if (cal_block->map_data.dma_buf != NULL) {
  430. if (cal_block->cma_mem)
  431. msm_audio_ion_free_cma(cal_block->map_data.dma_buf);
  432. else
  433. msm_audio_ion_free(cal_block->map_data.dma_buf);
  434. cal_block->map_data.dma_buf = NULL;
  435. }
  436. kfree(cal_block);
  437. done:
  438. return;
  439. }
  440. static void destroy_all_cal_blocks(struct cal_type_data *cal_type)
  441. {
  442. int ret = 0;
  443. struct list_head *ptr, *next;
  444. struct cal_block_data *cal_block;
  445. list_for_each_safe(ptr, next,
  446. &cal_type->cal_blocks) {
  447. cal_block = list_entry(ptr,
  448. struct cal_block_data, list);
  449. ret = unmap_memory(cal_type, cal_block);
  450. if (ret < 0) {
  451. pr_err("%s: unmap_memory failed, cal type %d, ret = %d!\n",
  452. __func__,
  453. cal_type->info.reg.cal_type,
  454. ret);
  455. }
  456. delete_cal_block(cal_block);
  457. cal_block = NULL;
  458. }
  459. }
  460. static void destroy_cal_type_data(struct cal_type_data *cal_type)
  461. {
  462. if (cal_type == NULL)
  463. goto done;
  464. destroy_all_cal_blocks(cal_type);
  465. list_del(&cal_type->cal_blocks);
  466. kfree(cal_type);
  467. done:
  468. return;
  469. }
  470. /**
  471. * cal_utils_destroy_cal_types -
  472. * Destroys cal types and deregister from cal info
  473. *
  474. * @num_cal_types: number of cal types
  475. * @cal_type: cal type pointer with cal info
  476. *
  477. */
  478. void cal_utils_destroy_cal_types(int num_cal_types,
  479. struct cal_type_data **cal_type)
  480. {
  481. int i;
  482. pr_debug("%s\n", __func__);
  483. if (cal_type == NULL) {
  484. pr_err("%s: cal_type is NULL!\n", __func__);
  485. goto done;
  486. } else if ((num_cal_types <= 0) ||
  487. (num_cal_types > MAX_CAL_TYPES)) {
  488. pr_err("%s: num_cal_types of %d is Invalid!\n",
  489. __func__, num_cal_types);
  490. goto done;
  491. }
  492. for (i = 0; i < num_cal_types; i++) {
  493. audio_cal_deregister(1, &cal_type[i]->info.reg);
  494. destroy_cal_type_data(cal_type[i]);
  495. cal_type[i] = NULL;
  496. }
  497. done:
  498. return;
  499. }
  500. EXPORT_SYMBOL(cal_utils_destroy_cal_types);
  501. /**
  502. * cal_utils_get_only_cal_block
  503. *
  504. * @cal_type: pointer to the cal type
  505. *
  506. * Returns cal_block structure
  507. */
  508. struct cal_block_data *cal_utils_get_only_cal_block(
  509. struct cal_type_data *cal_type)
  510. {
  511. struct list_head *ptr, *next;
  512. struct cal_block_data *cal_block = NULL;
  513. if (cal_type == NULL)
  514. goto done;
  515. list_for_each_safe(ptr, next,
  516. &cal_type->cal_blocks) {
  517. cal_block = list_entry(ptr,
  518. struct cal_block_data, list);
  519. break;
  520. }
  521. done:
  522. return cal_block;
  523. }
  524. EXPORT_SYMBOL(cal_utils_get_only_cal_block);
  525. /**
  526. * cal_utils_get_only_cal_block
  527. *
  528. * @cal_block: pointer to cal block struct
  529. * @user_data: pointer to user data
  530. *
  531. * Returns true on match
  532. */
  533. bool cal_utils_match_buf_num(struct cal_block_data *cal_block,
  534. void *user_data)
  535. {
  536. bool ret = false;
  537. struct audio_cal_type_basic *data = user_data;
  538. if (cal_block->buffer_number == data->cal_hdr.buffer_number)
  539. ret = true;
  540. return ret;
  541. }
  542. EXPORT_SYMBOL(cal_utils_match_buf_num);
  543. static struct cal_block_data *get_matching_cal_block(
  544. struct cal_type_data *cal_type,
  545. void *data)
  546. {
  547. struct list_head *ptr, *next;
  548. struct cal_block_data *cal_block = NULL;
  549. list_for_each_safe(ptr, next,
  550. &cal_type->cal_blocks) {
  551. cal_block = list_entry(ptr,
  552. struct cal_block_data, list);
  553. if (cal_type->info.cal_util_callbacks.
  554. match_block(cal_block, data))
  555. return cal_block;
  556. }
  557. return NULL;
  558. }
  559. static int cal_block_ion_alloc(struct cal_block_data *cal_block)
  560. {
  561. int ret = 0;
  562. if (cal_block == NULL) {
  563. pr_err("%s: cal_block is NULL!\n", __func__);
  564. ret = -EINVAL;
  565. goto done;
  566. }
  567. if (cal_block->cma_mem) {
  568. ret = msm_audio_ion_import_cma(&cal_block->map_data.dma_buf,
  569. cal_block->map_data.ion_map_handle,
  570. NULL, 0,
  571. &cal_block->cal_data.paddr,
  572. &cal_block->map_data.map_size,
  573. &cal_block->cal_data.kvaddr);
  574. } else {
  575. ret = msm_audio_ion_import(&cal_block->map_data.dma_buf,
  576. cal_block->map_data.ion_map_handle,
  577. NULL, 0,
  578. &cal_block->cal_data.paddr,
  579. &cal_block->map_data.map_size,
  580. &cal_block->cal_data.kvaddr);
  581. }
  582. if (ret) {
  583. pr_err("%s: audio ION import failed, rc = %d\n",
  584. __func__, ret);
  585. ret = -ENOMEM;
  586. goto done;
  587. }
  588. done:
  589. return ret;
  590. }
  591. static struct cal_block_data *create_cal_block(struct cal_type_data *cal_type,
  592. struct audio_cal_type_basic *basic_cal,
  593. size_t client_info_size, void *client_info)
  594. {
  595. struct cal_block_data *cal_block = NULL;
  596. if (cal_type == NULL) {
  597. pr_err("%s: cal_type is NULL!\n", __func__);
  598. goto done;
  599. } else if (basic_cal == NULL) {
  600. pr_err("%s: basic_cal is NULL!\n", __func__);
  601. goto done;
  602. }
  603. cal_block = kzalloc(sizeof(*cal_block),
  604. GFP_KERNEL);
  605. if (cal_block == NULL)
  606. goto done;
  607. INIT_LIST_HEAD(&cal_block->list);
  608. cal_block->map_data.ion_map_handle = basic_cal->cal_data.mem_handle;
  609. cal_block->cma_mem = basic_cal->cal_data.cma_mem;
  610. if (basic_cal->cal_data.mem_handle > 0) {
  611. if (cal_block_ion_alloc(cal_block)) {
  612. pr_err("%s: cal_block_ion_alloc failed!\n",
  613. __func__);
  614. goto err;
  615. }
  616. }
  617. if (client_info_size > 0) {
  618. cal_block->client_info_size = client_info_size;
  619. cal_block->client_info = kmalloc(client_info_size, GFP_KERNEL);
  620. if (cal_block->client_info == NULL) {
  621. pr_err("%s: could not allocats client_info!\n",
  622. __func__);
  623. goto err;
  624. }
  625. if (client_info != NULL)
  626. memcpy(cal_block->client_info, client_info,
  627. client_info_size);
  628. }
  629. cal_block->cal_info = kzalloc(
  630. get_cal_info_size(cal_type->info.reg.cal_type),
  631. GFP_KERNEL);
  632. if (cal_block->cal_info == NULL) {
  633. pr_err("%s: could not allocats cal_info!\n",
  634. __func__);
  635. goto err;
  636. }
  637. cal_block->buffer_number = basic_cal->cal_hdr.buffer_number;
  638. list_add_tail(&cal_block->list, &cal_type->cal_blocks);
  639. pr_debug("%s: created block for cal type %d, buf num %d, map handle %d, map size %zd paddr 0x%pK!\n",
  640. __func__, cal_type->info.reg.cal_type,
  641. cal_block->buffer_number,
  642. cal_block->map_data.ion_map_handle,
  643. cal_block->map_data.map_size,
  644. &cal_block->cal_data.paddr);
  645. done:
  646. return cal_block;
  647. err:
  648. kfree(cal_block->cal_info);
  649. cal_block->cal_info = NULL;
  650. kfree(cal_block->client_info);
  651. cal_block->client_info = NULL;
  652. kfree(cal_block);
  653. cal_block = NULL;
  654. return cal_block;
  655. }
  656. void cal_utils_clear_cal_block_q6maps(int num_cal_types,
  657. struct cal_type_data **cal_type)
  658. {
  659. int i = 0;
  660. struct list_head *ptr, *next;
  661. struct cal_block_data *cal_block;
  662. pr_debug("%s\n", __func__);
  663. if (cal_type == NULL) {
  664. pr_err("%s: cal_type is NULL!\n", __func__);
  665. goto done;
  666. } else if ((num_cal_types <= 0) ||
  667. (num_cal_types > MAX_CAL_TYPES)) {
  668. pr_err("%s: num_cal_types of %d is Invalid!\n",
  669. __func__, num_cal_types);
  670. goto done;
  671. }
  672. for (; i < num_cal_types; i++) {
  673. if (cal_type[i] == NULL)
  674. continue;
  675. mutex_lock(&cal_type[i]->lock);
  676. list_for_each_safe(ptr, next,
  677. &cal_type[i]->cal_blocks) {
  678. cal_block = list_entry(ptr,
  679. struct cal_block_data, list);
  680. cal_block->map_data.q6map_handle = 0;
  681. }
  682. mutex_unlock(&cal_type[i]->lock);
  683. }
  684. done:
  685. return;
  686. }
  687. static int realloc_memory(struct cal_block_data *cal_block)
  688. {
  689. int ret = 0;
  690. if (cal_block->cma_mem)
  691. msm_audio_ion_free_cma(cal_block->map_data.dma_buf);
  692. else
  693. msm_audio_ion_free(cal_block->map_data.dma_buf);
  694. cal_block->map_data.dma_buf = NULL;
  695. cal_block->cal_data.size = 0;
  696. ret = cal_block_ion_alloc(cal_block);
  697. if (ret < 0)
  698. pr_err("%s: realloc_memory failed!\n",
  699. __func__);
  700. return ret;
  701. }
  702. static int map_memory(struct cal_type_data *cal_type,
  703. struct cal_block_data *cal_block)
  704. {
  705. int ret = 0;
  706. if (cal_type->info.cal_util_callbacks.map_cal != NULL) {
  707. if ((cal_block->map_data.ion_map_handle < 0) ||
  708. (cal_block->map_data.map_size <= 0) ||
  709. (cal_block->map_data.q6map_handle != 0)) {
  710. goto done;
  711. }
  712. pr_debug("%s: cal type %d call map\n",
  713. __func__, cal_type->info.reg.cal_type);
  714. ret = cal_type->info.cal_util_callbacks.
  715. map_cal(cal_type->info.reg.cal_type, cal_block);
  716. if (ret < 0) {
  717. pr_err("%s: map_cal failed, cal type %d, ret = %d!\n",
  718. __func__, cal_type->info.reg.cal_type,
  719. ret);
  720. goto done;
  721. }
  722. }
  723. done:
  724. return ret;
  725. }
  726. static int unmap_memory(struct cal_type_data *cal_type,
  727. struct cal_block_data *cal_block)
  728. {
  729. int ret = 0;
  730. if (cal_type->info.cal_util_callbacks.unmap_cal != NULL) {
  731. if ((cal_block->map_data.ion_map_handle < 0) ||
  732. (cal_block->map_data.map_size <= 0) ||
  733. (cal_block->map_data.q6map_handle == 0)) {
  734. goto done;
  735. }
  736. pr_debug("%s: cal type %d call unmap\n",
  737. __func__, cal_type->info.reg.cal_type);
  738. ret = cal_type->info.cal_util_callbacks.
  739. unmap_cal(cal_type->info.reg.cal_type, cal_block);
  740. if (ret < 0) {
  741. pr_err("%s: unmap_cal failed, cal type %d, ret = %d!\n",
  742. __func__, cal_type->info.reg.cal_type,
  743. ret);
  744. goto done;
  745. }
  746. }
  747. done:
  748. return ret;
  749. }
  750. /**
  751. * cal_utils_alloc_cal
  752. *
  753. * @data_size: size of data to allocate
  754. * @data: data pointer
  755. * @cal_type: pointer to the cal type
  756. * @client_info_size: client info size
  757. * @client_info: pointer to client info
  758. *
  759. * Returns 0 on success, appropriate error code otherwise
  760. */
  761. int cal_utils_alloc_cal(size_t data_size, void *data,
  762. struct cal_type_data *cal_type,
  763. size_t client_info_size, void *client_info)
  764. {
  765. int ret = 0;
  766. struct cal_block_data *cal_block;
  767. struct audio_cal_type_alloc *alloc_data = data;
  768. pr_debug("%s\n", __func__);
  769. if (cal_type == NULL) {
  770. pr_err("%s: cal_type is NULL!\n",
  771. __func__);
  772. ret = -EINVAL;
  773. goto done;
  774. }
  775. if (data_size < sizeof(struct audio_cal_type_alloc)) {
  776. pr_err("%s: data_size of %zd does not equal alloc struct size of %zd!\n",
  777. __func__, data_size,
  778. sizeof(struct audio_cal_type_alloc));
  779. ret = -EINVAL;
  780. goto done;
  781. }
  782. if ((client_info_size > 0) && (client_info == NULL)) {
  783. pr_err("%s: User info pointer is NULL but size is %zd!\n",
  784. __func__, client_info_size);
  785. ret = -EINVAL;
  786. goto done;
  787. }
  788. if (alloc_data->cal_data.mem_handle < 0) {
  789. pr_err("%s: mem_handle %d invalid!\n",
  790. __func__, alloc_data->cal_data.mem_handle);
  791. ret = -EINVAL;
  792. goto done;
  793. }
  794. mutex_lock(&cal_type->lock);
  795. cal_block = get_matching_cal_block(cal_type,
  796. data);
  797. if (cal_block != NULL) {
  798. cal_block->cma_mem = alloc_data->cal_data.cma_mem;
  799. ret = unmap_memory(cal_type, cal_block);
  800. if (ret < 0)
  801. goto err;
  802. ret = realloc_memory(cal_block);
  803. if (ret < 0)
  804. goto err;
  805. } else {
  806. cal_block = create_cal_block(cal_type,
  807. (struct audio_cal_type_basic *)alloc_data,
  808. client_info_size, client_info);
  809. if (cal_block == NULL) {
  810. pr_err("%s: create_cal_block failed for %d!\n",
  811. __func__, alloc_data->cal_data.mem_handle);
  812. ret = -EINVAL;
  813. goto err;
  814. }
  815. }
  816. ret = map_memory(cal_type, cal_block);
  817. if (ret < 0)
  818. goto err;
  819. err:
  820. mutex_unlock(&cal_type->lock);
  821. done:
  822. return ret;
  823. }
  824. EXPORT_SYMBOL(cal_utils_alloc_cal);
  825. /**
  826. * cal_utils_dealloc_cal
  827. *
  828. * @data_size: size of data to allocate
  829. * @data: data pointer
  830. * @cal_type: pointer to the cal type
  831. *
  832. * Returns 0 on success, appropriate error code otherwise
  833. */
  834. int cal_utils_dealloc_cal(size_t data_size, void *data,
  835. struct cal_type_data *cal_type)
  836. {
  837. int ret = 0;
  838. struct cal_block_data *cal_block;
  839. struct audio_cal_type_dealloc *dealloc_data = data;
  840. pr_debug("%s\n", __func__);
  841. if (cal_type == NULL) {
  842. pr_err("%s: cal_type is NULL!\n",
  843. __func__);
  844. ret = -EINVAL;
  845. goto done;
  846. }
  847. if (data_size < sizeof(struct audio_cal_type_dealloc)) {
  848. pr_err("%s: data_size of %zd does not equal struct size of %zd!\n",
  849. __func__, data_size,
  850. sizeof(struct audio_cal_type_dealloc));
  851. ret = -EINVAL;
  852. goto done;
  853. }
  854. if ((dealloc_data->cal_data.mem_handle == -1) &&
  855. (dealloc_data->cal_hdr.buffer_number == ALL_CAL_BLOCKS)) {
  856. destroy_all_cal_blocks(cal_type);
  857. goto done;
  858. }
  859. if (dealloc_data->cal_data.mem_handle < 0) {
  860. pr_err("%s: mem_handle %d invalid!\n",
  861. __func__, dealloc_data->cal_data.mem_handle);
  862. ret = -EINVAL;
  863. goto done;
  864. }
  865. mutex_lock(&cal_type->lock);
  866. cal_block = get_matching_cal_block(
  867. cal_type,
  868. data);
  869. if (cal_block == NULL) {
  870. pr_err("%s: allocation does not exist for %d!\n",
  871. __func__, dealloc_data->cal_data.mem_handle);
  872. ret = -EINVAL;
  873. goto err;
  874. }
  875. ret = unmap_memory(cal_type, cal_block);
  876. if (ret < 0)
  877. goto err;
  878. delete_cal_block(cal_block);
  879. err:
  880. mutex_unlock(&cal_type->lock);
  881. done:
  882. return ret;
  883. }
  884. EXPORT_SYMBOL(cal_utils_dealloc_cal);
  885. /**
  886. * cal_utils_set_cal
  887. *
  888. * @data_size: size of data to allocate
  889. * @data: data pointer
  890. * @cal_type: pointer to the cal type
  891. * @client_info_size: client info size
  892. * @client_info: pointer to client info
  893. *
  894. * Returns 0 on success, appropriate error code otherwise
  895. */
  896. int cal_utils_set_cal(size_t data_size, void *data,
  897. struct cal_type_data *cal_type,
  898. size_t client_info_size, void *client_info)
  899. {
  900. int ret = 0;
  901. struct cal_block_data *cal_block;
  902. struct audio_cal_type_basic *basic_data = data;
  903. pr_debug("%s\n", __func__);
  904. if (cal_type == NULL) {
  905. pr_err("%s: cal_type is NULL!\n",
  906. __func__);
  907. ret = -EINVAL;
  908. goto done;
  909. }
  910. if ((client_info_size > 0) && (client_info == NULL)) {
  911. pr_err("%s: User info pointer is NULL but size is %zd!\n",
  912. __func__, client_info_size);
  913. ret = -EINVAL;
  914. goto done;
  915. }
  916. if ((data_size > get_user_cal_type_size(
  917. cal_type->info.reg.cal_type)) || (data_size < 0)) {
  918. pr_err("%s: cal_type %d, data_size of %zd is invalid, expecting %zd!\n",
  919. __func__, cal_type->info.reg.cal_type, data_size,
  920. get_user_cal_type_size(cal_type->info.reg.cal_type));
  921. ret = -EINVAL;
  922. goto done;
  923. }
  924. mutex_lock(&cal_type->lock);
  925. cal_block = get_matching_cal_block(
  926. cal_type,
  927. data);
  928. if (cal_block == NULL) {
  929. if (basic_data->cal_data.mem_handle > 0) {
  930. pr_err("%s: allocation does not exist for %d!\n",
  931. __func__, basic_data->cal_data.mem_handle);
  932. ret = -EINVAL;
  933. goto err;
  934. } else {
  935. cal_block = create_cal_block(
  936. cal_type,
  937. basic_data,
  938. client_info_size, client_info);
  939. if (cal_block == NULL) {
  940. pr_err("%s: create_cal_block failed for cal type %d!\n",
  941. __func__,
  942. cal_type->info.reg.cal_type);
  943. ret = -EINVAL;
  944. goto err;
  945. }
  946. }
  947. }
  948. ret = map_memory(cal_type, cal_block);
  949. if (ret < 0)
  950. goto err;
  951. cal_block->cal_data.size = basic_data->cal_data.cal_size;
  952. if (client_info_size > 0) {
  953. memcpy(cal_block->client_info,
  954. client_info,
  955. client_info_size);
  956. }
  957. memcpy(cal_block->cal_info,
  958. ((uint8_t *)data + sizeof(struct audio_cal_type_basic)),
  959. data_size - sizeof(struct audio_cal_type_basic));
  960. /* reset buffer stale flag */
  961. cal_block->cal_stale = false;
  962. err:
  963. mutex_unlock(&cal_type->lock);
  964. done:
  965. return ret;
  966. }
  967. EXPORT_SYMBOL(cal_utils_set_cal);
  968. /**
  969. * cal_utils_mark_cal_used
  970. *
  971. * @cal_block: pointer to cal block
  972. */
  973. void cal_utils_mark_cal_used(struct cal_block_data *cal_block)
  974. {
  975. if (cal_block)
  976. cal_block->cal_stale = true;
  977. }
  978. EXPORT_SYMBOL(cal_utils_mark_cal_used);
  979. /**
  980. * cal_utils_is_cal_stale
  981. *
  982. * @cal_block: pointer to cal block
  983. *
  984. * Returns true if cal block is stale, false otherwise
  985. */
  986. bool cal_utils_is_cal_stale(struct cal_block_data *cal_block)
  987. {
  988. if ((cal_block) && (cal_block->cal_stale))
  989. return true;
  990. return false;
  991. }
  992. EXPORT_SYMBOL(cal_utils_is_cal_stale);