audio_cal_utils.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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. msm_audio_ion_free(cal_block->map_data.dma_buf);
  431. cal_block->map_data.dma_buf = NULL;
  432. }
  433. kfree(cal_block);
  434. done:
  435. return;
  436. }
  437. static void destroy_all_cal_blocks(struct cal_type_data *cal_type)
  438. {
  439. int ret = 0;
  440. struct list_head *ptr, *next;
  441. struct cal_block_data *cal_block;
  442. list_for_each_safe(ptr, next,
  443. &cal_type->cal_blocks) {
  444. cal_block = list_entry(ptr,
  445. struct cal_block_data, list);
  446. ret = unmap_memory(cal_type, cal_block);
  447. if (ret < 0) {
  448. pr_err("%s: unmap_memory failed, cal type %d, ret = %d!\n",
  449. __func__,
  450. cal_type->info.reg.cal_type,
  451. ret);
  452. }
  453. delete_cal_block(cal_block);
  454. cal_block = NULL;
  455. }
  456. }
  457. static void destroy_cal_type_data(struct cal_type_data *cal_type)
  458. {
  459. if (cal_type == NULL)
  460. goto done;
  461. destroy_all_cal_blocks(cal_type);
  462. list_del(&cal_type->cal_blocks);
  463. kfree(cal_type);
  464. done:
  465. return;
  466. }
  467. /**
  468. * cal_utils_destroy_cal_types -
  469. * Destroys cal types and deregister from cal info
  470. *
  471. * @num_cal_types: number of cal types
  472. * @cal_type: cal type pointer with cal info
  473. *
  474. */
  475. void cal_utils_destroy_cal_types(int num_cal_types,
  476. struct cal_type_data **cal_type)
  477. {
  478. int i;
  479. pr_debug("%s\n", __func__);
  480. if (cal_type == NULL) {
  481. pr_err("%s: cal_type is NULL!\n", __func__);
  482. goto done;
  483. } else if ((num_cal_types <= 0) ||
  484. (num_cal_types > MAX_CAL_TYPES)) {
  485. pr_err("%s: num_cal_types of %d is Invalid!\n",
  486. __func__, num_cal_types);
  487. goto done;
  488. }
  489. for (i = 0; i < num_cal_types; i++) {
  490. audio_cal_deregister(1, &cal_type[i]->info.reg);
  491. destroy_cal_type_data(cal_type[i]);
  492. cal_type[i] = NULL;
  493. }
  494. done:
  495. return;
  496. }
  497. EXPORT_SYMBOL(cal_utils_destroy_cal_types);
  498. /**
  499. * cal_utils_get_only_cal_block
  500. *
  501. * @cal_type: pointer to the cal type
  502. *
  503. * Returns cal_block structure
  504. */
  505. struct cal_block_data *cal_utils_get_only_cal_block(
  506. struct cal_type_data *cal_type)
  507. {
  508. struct list_head *ptr, *next;
  509. struct cal_block_data *cal_block = NULL;
  510. if (cal_type == NULL)
  511. goto done;
  512. list_for_each_safe(ptr, next,
  513. &cal_type->cal_blocks) {
  514. cal_block = list_entry(ptr,
  515. struct cal_block_data, list);
  516. break;
  517. }
  518. done:
  519. return cal_block;
  520. }
  521. EXPORT_SYMBOL(cal_utils_get_only_cal_block);
  522. /**
  523. * cal_utils_get_only_cal_block
  524. *
  525. * @cal_block: pointer to cal block struct
  526. * @user_data: pointer to user data
  527. *
  528. * Returns true on match
  529. */
  530. bool cal_utils_match_buf_num(struct cal_block_data *cal_block,
  531. void *user_data)
  532. {
  533. bool ret = false;
  534. struct audio_cal_type_basic *data = user_data;
  535. if (cal_block->buffer_number == data->cal_hdr.buffer_number)
  536. ret = true;
  537. return ret;
  538. }
  539. EXPORT_SYMBOL(cal_utils_match_buf_num);
  540. static struct cal_block_data *get_matching_cal_block(
  541. struct cal_type_data *cal_type,
  542. void *data)
  543. {
  544. struct list_head *ptr, *next;
  545. struct cal_block_data *cal_block = NULL;
  546. list_for_each_safe(ptr, next,
  547. &cal_type->cal_blocks) {
  548. cal_block = list_entry(ptr,
  549. struct cal_block_data, list);
  550. if (cal_type->info.cal_util_callbacks.
  551. match_block(cal_block, data))
  552. return cal_block;
  553. }
  554. return NULL;
  555. }
  556. static int cal_block_ion_alloc(struct cal_block_data *cal_block)
  557. {
  558. int ret = 0;
  559. if (cal_block == NULL) {
  560. pr_err("%s: cal_block is NULL!\n", __func__);
  561. ret = -EINVAL;
  562. goto done;
  563. }
  564. ret = msm_audio_ion_import(&cal_block->map_data.dma_buf,
  565. cal_block->map_data.ion_map_handle,
  566. NULL, 0,
  567. &cal_block->cal_data.paddr,
  568. &cal_block->map_data.map_size,
  569. &cal_block->cal_data.kvaddr);
  570. if (ret) {
  571. pr_err("%s: audio ION import failed, rc = %d\n",
  572. __func__, ret);
  573. ret = -ENOMEM;
  574. goto done;
  575. }
  576. done:
  577. return ret;
  578. }
  579. static struct cal_block_data *create_cal_block(struct cal_type_data *cal_type,
  580. struct audio_cal_type_basic *basic_cal,
  581. size_t client_info_size, void *client_info)
  582. {
  583. struct cal_block_data *cal_block = NULL;
  584. if (cal_type == NULL) {
  585. pr_err("%s: cal_type is NULL!\n", __func__);
  586. goto done;
  587. } else if (basic_cal == NULL) {
  588. pr_err("%s: basic_cal is NULL!\n", __func__);
  589. goto done;
  590. }
  591. cal_block = kzalloc(sizeof(*cal_block),
  592. GFP_KERNEL);
  593. if (cal_block == NULL)
  594. goto done;
  595. INIT_LIST_HEAD(&cal_block->list);
  596. cal_block->map_data.ion_map_handle = basic_cal->cal_data.mem_handle;
  597. if (basic_cal->cal_data.mem_handle > 0) {
  598. if (cal_block_ion_alloc(cal_block)) {
  599. pr_err("%s: cal_block_ion_alloc failed!\n",
  600. __func__);
  601. goto err;
  602. }
  603. }
  604. if (client_info_size > 0) {
  605. cal_block->client_info_size = client_info_size;
  606. cal_block->client_info = kmalloc(client_info_size, GFP_KERNEL);
  607. if (cal_block->client_info == NULL) {
  608. pr_err("%s: could not allocats client_info!\n",
  609. __func__);
  610. goto err;
  611. }
  612. if (client_info != NULL)
  613. memcpy(cal_block->client_info, client_info,
  614. client_info_size);
  615. }
  616. cal_block->cal_info = kzalloc(
  617. get_cal_info_size(cal_type->info.reg.cal_type),
  618. GFP_KERNEL);
  619. if (cal_block->cal_info == NULL) {
  620. pr_err("%s: could not allocats cal_info!\n",
  621. __func__);
  622. goto err;
  623. }
  624. cal_block->buffer_number = basic_cal->cal_hdr.buffer_number;
  625. list_add_tail(&cal_block->list, &cal_type->cal_blocks);
  626. pr_debug("%s: created block for cal type %d, buf num %d, map handle %d, map size %zd paddr 0x%pK!\n",
  627. __func__, cal_type->info.reg.cal_type,
  628. cal_block->buffer_number,
  629. cal_block->map_data.ion_map_handle,
  630. cal_block->map_data.map_size,
  631. &cal_block->cal_data.paddr);
  632. done:
  633. return cal_block;
  634. err:
  635. kfree(cal_block->cal_info);
  636. cal_block->cal_info = NULL;
  637. kfree(cal_block->client_info);
  638. cal_block->client_info = NULL;
  639. kfree(cal_block);
  640. cal_block = NULL;
  641. return cal_block;
  642. }
  643. void cal_utils_clear_cal_block_q6maps(int num_cal_types,
  644. struct cal_type_data **cal_type)
  645. {
  646. int i = 0;
  647. struct list_head *ptr, *next;
  648. struct cal_block_data *cal_block;
  649. pr_debug("%s\n", __func__);
  650. if (cal_type == NULL) {
  651. pr_err("%s: cal_type is NULL!\n", __func__);
  652. goto done;
  653. } else if ((num_cal_types <= 0) ||
  654. (num_cal_types > MAX_CAL_TYPES)) {
  655. pr_err("%s: num_cal_types of %d is Invalid!\n",
  656. __func__, num_cal_types);
  657. goto done;
  658. }
  659. for (; i < num_cal_types; i++) {
  660. if (cal_type[i] == NULL)
  661. continue;
  662. mutex_lock(&cal_type[i]->lock);
  663. list_for_each_safe(ptr, next,
  664. &cal_type[i]->cal_blocks) {
  665. cal_block = list_entry(ptr,
  666. struct cal_block_data, list);
  667. cal_block->map_data.q6map_handle = 0;
  668. }
  669. mutex_unlock(&cal_type[i]->lock);
  670. }
  671. done:
  672. return;
  673. }
  674. static int realloc_memory(struct cal_block_data *cal_block)
  675. {
  676. int ret = 0;
  677. msm_audio_ion_free(cal_block->map_data.dma_buf);
  678. cal_block->map_data.dma_buf = NULL;
  679. cal_block->cal_data.size = 0;
  680. ret = cal_block_ion_alloc(cal_block);
  681. if (ret < 0)
  682. pr_err("%s: realloc_memory failed!\n",
  683. __func__);
  684. return ret;
  685. }
  686. static int map_memory(struct cal_type_data *cal_type,
  687. struct cal_block_data *cal_block)
  688. {
  689. int ret = 0;
  690. if (cal_type->info.cal_util_callbacks.map_cal != NULL) {
  691. if ((cal_block->map_data.ion_map_handle < 0) ||
  692. (cal_block->map_data.map_size <= 0) ||
  693. (cal_block->map_data.q6map_handle != 0)) {
  694. goto done;
  695. }
  696. pr_debug("%s: cal type %d call map\n",
  697. __func__, cal_type->info.reg.cal_type);
  698. ret = cal_type->info.cal_util_callbacks.
  699. map_cal(cal_type->info.reg.cal_type, cal_block);
  700. if (ret < 0) {
  701. pr_err("%s: map_cal failed, cal type %d, ret = %d!\n",
  702. __func__, cal_type->info.reg.cal_type,
  703. ret);
  704. goto done;
  705. }
  706. }
  707. done:
  708. return ret;
  709. }
  710. static int unmap_memory(struct cal_type_data *cal_type,
  711. struct cal_block_data *cal_block)
  712. {
  713. int ret = 0;
  714. if (cal_type->info.cal_util_callbacks.unmap_cal != NULL) {
  715. if ((cal_block->map_data.ion_map_handle < 0) ||
  716. (cal_block->map_data.map_size <= 0) ||
  717. (cal_block->map_data.q6map_handle == 0)) {
  718. goto done;
  719. }
  720. pr_debug("%s: cal type %d call unmap\n",
  721. __func__, cal_type->info.reg.cal_type);
  722. ret = cal_type->info.cal_util_callbacks.
  723. unmap_cal(cal_type->info.reg.cal_type, cal_block);
  724. if (ret < 0) {
  725. pr_err("%s: unmap_cal failed, cal type %d, ret = %d!\n",
  726. __func__, cal_type->info.reg.cal_type,
  727. ret);
  728. goto done;
  729. }
  730. }
  731. done:
  732. return ret;
  733. }
  734. /**
  735. * cal_utils_alloc_cal
  736. *
  737. * @data_size: size of data to allocate
  738. * @data: data pointer
  739. * @cal_type: pointer to the cal type
  740. * @client_info_size: client info size
  741. * @client_info: pointer to client info
  742. *
  743. * Returns 0 on success, appropriate error code otherwise
  744. */
  745. int cal_utils_alloc_cal(size_t data_size, void *data,
  746. struct cal_type_data *cal_type,
  747. size_t client_info_size, void *client_info)
  748. {
  749. int ret = 0;
  750. struct cal_block_data *cal_block;
  751. struct audio_cal_type_alloc *alloc_data = data;
  752. pr_debug("%s\n", __func__);
  753. if (cal_type == NULL) {
  754. pr_err("%s: cal_type is NULL!\n",
  755. __func__);
  756. ret = -EINVAL;
  757. goto done;
  758. }
  759. if (data_size < sizeof(struct audio_cal_type_alloc)) {
  760. pr_err("%s: data_size of %zd does not equal alloc struct size of %zd!\n",
  761. __func__, data_size,
  762. sizeof(struct audio_cal_type_alloc));
  763. ret = -EINVAL;
  764. goto done;
  765. }
  766. if ((client_info_size > 0) && (client_info == NULL)) {
  767. pr_err("%s: User info pointer is NULL but size is %zd!\n",
  768. __func__, client_info_size);
  769. ret = -EINVAL;
  770. goto done;
  771. }
  772. if (alloc_data->cal_data.mem_handle < 0) {
  773. pr_err("%s: mem_handle %d invalid!\n",
  774. __func__, alloc_data->cal_data.mem_handle);
  775. ret = -EINVAL;
  776. goto done;
  777. }
  778. mutex_lock(&cal_type->lock);
  779. cal_block = get_matching_cal_block(cal_type,
  780. data);
  781. if (cal_block != NULL) {
  782. ret = unmap_memory(cal_type, cal_block);
  783. if (ret < 0)
  784. goto err;
  785. ret = realloc_memory(cal_block);
  786. if (ret < 0)
  787. goto err;
  788. } else {
  789. cal_block = create_cal_block(cal_type,
  790. (struct audio_cal_type_basic *)alloc_data,
  791. client_info_size, client_info);
  792. if (cal_block == NULL) {
  793. pr_err("%s: create_cal_block failed for %d!\n",
  794. __func__, alloc_data->cal_data.mem_handle);
  795. ret = -EINVAL;
  796. goto err;
  797. }
  798. }
  799. ret = map_memory(cal_type, cal_block);
  800. if (ret < 0)
  801. goto err;
  802. err:
  803. mutex_unlock(&cal_type->lock);
  804. done:
  805. return ret;
  806. }
  807. EXPORT_SYMBOL(cal_utils_alloc_cal);
  808. /**
  809. * cal_utils_dealloc_cal
  810. *
  811. * @data_size: size of data to allocate
  812. * @data: data pointer
  813. * @cal_type: pointer to the cal type
  814. *
  815. * Returns 0 on success, appropriate error code otherwise
  816. */
  817. int cal_utils_dealloc_cal(size_t data_size, void *data,
  818. struct cal_type_data *cal_type)
  819. {
  820. int ret = 0;
  821. struct cal_block_data *cal_block;
  822. struct audio_cal_type_dealloc *dealloc_data = data;
  823. pr_debug("%s\n", __func__);
  824. if (cal_type == NULL) {
  825. pr_err("%s: cal_type is NULL!\n",
  826. __func__);
  827. ret = -EINVAL;
  828. goto done;
  829. }
  830. if (data_size < sizeof(struct audio_cal_type_dealloc)) {
  831. pr_err("%s: data_size of %zd does not equal struct size of %zd!\n",
  832. __func__, data_size,
  833. sizeof(struct audio_cal_type_dealloc));
  834. ret = -EINVAL;
  835. goto done;
  836. }
  837. if ((dealloc_data->cal_data.mem_handle == -1) &&
  838. (dealloc_data->cal_hdr.buffer_number == ALL_CAL_BLOCKS)) {
  839. destroy_all_cal_blocks(cal_type);
  840. goto done;
  841. }
  842. if (dealloc_data->cal_data.mem_handle < 0) {
  843. pr_err("%s: mem_handle %d invalid!\n",
  844. __func__, dealloc_data->cal_data.mem_handle);
  845. ret = -EINVAL;
  846. goto done;
  847. }
  848. mutex_lock(&cal_type->lock);
  849. cal_block = get_matching_cal_block(
  850. cal_type,
  851. data);
  852. if (cal_block == NULL) {
  853. pr_err("%s: allocation does not exist for %d!\n",
  854. __func__, dealloc_data->cal_data.mem_handle);
  855. ret = -EINVAL;
  856. goto err;
  857. }
  858. ret = unmap_memory(cal_type, cal_block);
  859. if (ret < 0)
  860. goto err;
  861. delete_cal_block(cal_block);
  862. err:
  863. mutex_unlock(&cal_type->lock);
  864. done:
  865. return ret;
  866. }
  867. EXPORT_SYMBOL(cal_utils_dealloc_cal);
  868. /**
  869. * cal_utils_set_cal
  870. *
  871. * @data_size: size of data to allocate
  872. * @data: data pointer
  873. * @cal_type: pointer to the cal type
  874. * @client_info_size: client info size
  875. * @client_info: pointer to client info
  876. *
  877. * Returns 0 on success, appropriate error code otherwise
  878. */
  879. int cal_utils_set_cal(size_t data_size, void *data,
  880. struct cal_type_data *cal_type,
  881. size_t client_info_size, void *client_info)
  882. {
  883. int ret = 0;
  884. struct cal_block_data *cal_block;
  885. struct audio_cal_type_basic *basic_data = data;
  886. pr_debug("%s\n", __func__);
  887. if (cal_type == NULL) {
  888. pr_err("%s: cal_type is NULL!\n",
  889. __func__);
  890. ret = -EINVAL;
  891. goto done;
  892. }
  893. if ((client_info_size > 0) && (client_info == NULL)) {
  894. pr_err("%s: User info pointer is NULL but size is %zd!\n",
  895. __func__, client_info_size);
  896. ret = -EINVAL;
  897. goto done;
  898. }
  899. if ((data_size > get_user_cal_type_size(
  900. cal_type->info.reg.cal_type)) || (data_size < 0)) {
  901. pr_err("%s: cal_type %d, data_size of %zd is invalid, expecting %zd!\n",
  902. __func__, cal_type->info.reg.cal_type, data_size,
  903. get_user_cal_type_size(cal_type->info.reg.cal_type));
  904. ret = -EINVAL;
  905. goto done;
  906. }
  907. mutex_lock(&cal_type->lock);
  908. cal_block = get_matching_cal_block(
  909. cal_type,
  910. data);
  911. if (cal_block == NULL) {
  912. if (basic_data->cal_data.mem_handle > 0) {
  913. pr_err("%s: allocation does not exist for %d!\n",
  914. __func__, basic_data->cal_data.mem_handle);
  915. ret = -EINVAL;
  916. goto err;
  917. } else {
  918. cal_block = create_cal_block(
  919. cal_type,
  920. basic_data,
  921. client_info_size, client_info);
  922. if (cal_block == NULL) {
  923. pr_err("%s: create_cal_block failed for cal type %d!\n",
  924. __func__,
  925. cal_type->info.reg.cal_type);
  926. ret = -EINVAL;
  927. goto err;
  928. }
  929. }
  930. }
  931. ret = map_memory(cal_type, cal_block);
  932. if (ret < 0)
  933. goto err;
  934. cal_block->cal_data.size = basic_data->cal_data.cal_size;
  935. if (client_info_size > 0) {
  936. memcpy(cal_block->client_info,
  937. client_info,
  938. client_info_size);
  939. }
  940. memcpy(cal_block->cal_info,
  941. ((uint8_t *)data + sizeof(struct audio_cal_type_basic)),
  942. data_size - sizeof(struct audio_cal_type_basic));
  943. /* reset buffer stale flag */
  944. cal_block->cal_stale = false;
  945. err:
  946. mutex_unlock(&cal_type->lock);
  947. done:
  948. return ret;
  949. }
  950. EXPORT_SYMBOL(cal_utils_set_cal);
  951. /**
  952. * cal_utils_mark_cal_used
  953. *
  954. * @cal_block: pointer to cal block
  955. */
  956. void cal_utils_mark_cal_used(struct cal_block_data *cal_block)
  957. {
  958. if (cal_block)
  959. cal_block->cal_stale = true;
  960. }
  961. EXPORT_SYMBOL(cal_utils_mark_cal_used);
  962. /**
  963. * cal_utils_is_cal_stale
  964. *
  965. * @cal_block: pointer to cal block
  966. *
  967. * Returns true if cal block is stale, false otherwise
  968. */
  969. bool cal_utils_is_cal_stale(struct cal_block_data *cal_block)
  970. {
  971. if ((cal_block) && (cal_block->cal_stale))
  972. return true;
  973. return false;
  974. }
  975. EXPORT_SYMBOL(cal_utils_is_cal_stale);