audio_cal_utils.c 25 KB

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