audio_cal_utils.c 25 KB

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