12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved.
- */
- #include <linux/slab.h>
- #include <linux/fs.h>
- #include <linux/module.h>
- #include <linux/miscdevice.h>
- #include <linux/uaccess.h>
- #include <linux/mutex.h>
- #include <dsp/audio_cal_utils.h>
- static int unmap_memory(struct cal_type_data *cal_type,
- struct cal_block_data *cal_block);
- size_t get_cal_info_size(int32_t cal_type)
- {
- size_t size = 0;
- size_t size1 = 0, size2 = 0;
- switch (cal_type) {
- case CVP_VOC_RX_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_info_voc_top);
- break;
- case CVP_VOC_TX_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_info_voc_top);
- break;
- case CVP_VOCPROC_STATIC_CAL_TYPE:
- size = sizeof(struct audio_cal_info_vocproc);
- break;
- case CVP_VOCPROC_DYNAMIC_CAL_TYPE:
- size = sizeof(struct audio_cal_info_vocvol);
- break;
- case CVS_VOCSTRM_STATIC_CAL_TYPE:
- size = 0;
- break;
- case CVP_VOCDEV_CFG_CAL_TYPE:
- size = sizeof(struct audio_cal_info_vocdev_cfg);
- break;
- case CVP_VOCPROC_STATIC_COL_CAL_TYPE:
- size = sizeof(struct audio_cal_info_voc_col);
- break;
- case CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE:
- size = sizeof(struct audio_cal_info_voc_col);
- break;
- case CVS_VOCSTRM_STATIC_COL_CAL_TYPE:
- size = sizeof(struct audio_cal_info_voc_col);
- break;
- case ADM_TOPOLOGY_CAL_TYPE:
- case ADM_LSM_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_info_adm_top);
- break;
- case ADM_CUST_TOPOLOGY_CAL_TYPE:
- case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
- size = 0;
- break;
- case ADM_AUDPROC_CAL_TYPE:
- case ADM_LSM_AUDPROC_CAL_TYPE:
- case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
- size = sizeof(struct audio_cal_info_audproc);
- break;
- case ADM_AUDVOL_CAL_TYPE:
- case ADM_RTAC_AUDVOL_CAL_TYPE:
- size = sizeof(struct audio_cal_info_audvol);
- break;
- case ASM_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_info_asm_top);
- break;
- case ASM_CUST_TOPOLOGY_CAL_TYPE:
- size = 0;
- break;
- case ASM_AUDSTRM_CAL_TYPE:
- size = sizeof(struct audio_cal_info_audstrm);
- break;
- case AFE_TOPOLOGY_CAL_TYPE:
- case AFE_LSM_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_info_afe_top);
- break;
- case AFE_CUST_TOPOLOGY_CAL_TYPE:
- size = 0;
- break;
- case AFE_COMMON_RX_CAL_TYPE:
- size = sizeof(struct audio_cal_info_afe);
- break;
- case AFE_COMMON_TX_CAL_TYPE:
- case AFE_LSM_TX_CAL_TYPE:
- size = sizeof(struct audio_cal_info_afe);
- break;
- case AFE_FB_SPKR_PROT_CAL_TYPE:
- size = sizeof(struct audio_cal_info_spk_prot_cfg);
- break;
- case AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE:
- /*
- * Since get and set parameter structures are different in size
- * use the maximum size of get and set parameter structure
- */
- size1 = max(sizeof(struct audio_cal_info_sp_th_vi_ftm_cfg),
- sizeof(struct audio_cal_info_sp_th_vi_param));
- size2 = max(sizeof(struct audio_cal_info_sp_th_vi_v_vali_cfg),
- sizeof(struct audio_cal_info_sp_th_vi_v_vali_param));
- size = max(size1, size2);
- break;
- case AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE:
- /*
- * Since get and set parameter structures are different in size
- * use the maximum size of get and set parameter structure
- */
- size = max(sizeof(struct audio_cal_info_sp_ex_vi_ftm_cfg),
- sizeof(struct audio_cal_info_sp_ex_vi_param));
- break;
- case AFE_ANC_CAL_TYPE:
- size = 0;
- break;
- case AFE_AANC_CAL_TYPE:
- size = sizeof(struct audio_cal_info_aanc);
- break;
- case AFE_HW_DELAY_CAL_TYPE:
- size = sizeof(struct audio_cal_info_hw_delay);
- break;
- case AFE_SIDETONE_CAL_TYPE:
- size = sizeof(struct audio_cal_info_sidetone);
- break;
- case AFE_SIDETONE_IIR_CAL_TYPE:
- size = sizeof(struct audio_cal_info_sidetone_iir);
- break;
- case LSM_CUST_TOPOLOGY_CAL_TYPE:
- size = 0;
- break;
- case LSM_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_info_lsm_top);
- break;
- case ULP_LSM_TOPOLOGY_ID_CAL_TYPE:
- size = sizeof(struct audio_cal_info_lsm_top);
- break;
- case LSM_CAL_TYPE:
- size = sizeof(struct audio_cal_info_lsm);
- break;
- case ADM_RTAC_INFO_CAL_TYPE:
- size = 0;
- break;
- case VOICE_RTAC_INFO_CAL_TYPE:
- size = 0;
- break;
- case ADM_RTAC_APR_CAL_TYPE:
- size = 0;
- break;
- case ASM_RTAC_APR_CAL_TYPE:
- size = 0;
- break;
- case VOICE_RTAC_APR_CAL_TYPE:
- size = 0;
- break;
- case MAD_CAL_TYPE:
- size = 0;
- break;
- case ULP_AFE_CAL_TYPE:
- size = sizeof(struct audio_cal_info_afe);
- break;
- case ULP_LSM_CAL_TYPE:
- size = sizeof(struct audio_cal_info_lsm);
- break;
- case AUDIO_CORE_METAINFO_CAL_TYPE:
- size = sizeof(struct audio_cal_info_metainfo);
- break;
- case SRS_TRUMEDIA_CAL_TYPE:
- size = 0;
- break;
- default:
- pr_err("%s:Invalid cal type %d!",
- __func__, cal_type);
- }
- return size;
- }
- size_t get_user_cal_type_size(int32_t cal_type)
- {
- size_t size = 0;
- switch (cal_type) {
- case CVP_VOC_RX_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_voc_top);
- break;
- case CVP_VOC_TX_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_voc_top);
- break;
- case CVP_VOCPROC_STATIC_CAL_TYPE:
- size = sizeof(struct audio_cal_type_vocproc);
- break;
- case CVP_VOCPROC_DYNAMIC_CAL_TYPE:
- size = sizeof(struct audio_cal_type_vocvol);
- break;
- case CVS_VOCSTRM_STATIC_CAL_TYPE:
- size = sizeof(struct audio_cal_type_basic);
- break;
- case CVP_VOCDEV_CFG_CAL_TYPE:
- size = sizeof(struct audio_cal_type_vocdev_cfg);
- break;
- case CVP_VOCPROC_STATIC_COL_CAL_TYPE:
- case CVP_VOCPROC_DYNAMIC_COL_CAL_TYPE:
- case CVS_VOCSTRM_STATIC_COL_CAL_TYPE:
- size = sizeof(struct audio_cal_type_voc_col);
- break;
- case ADM_TOPOLOGY_CAL_TYPE:
- case ADM_LSM_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_adm_top);
- break;
- case ADM_CUST_TOPOLOGY_CAL_TYPE:
- case CORE_CUSTOM_TOPOLOGIES_CAL_TYPE:
- size = sizeof(struct audio_cal_type_basic);
- break;
- case ADM_AUDPROC_CAL_TYPE:
- case ADM_LSM_AUDPROC_CAL_TYPE:
- case ADM_LSM_AUDPROC_PERSISTENT_CAL_TYPE:
- size = sizeof(struct audio_cal_type_audproc);
- break;
- case ADM_AUDVOL_CAL_TYPE:
- case ADM_RTAC_AUDVOL_CAL_TYPE:
- size = sizeof(struct audio_cal_type_audvol);
- break;
- case ASM_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_asm_top);
- break;
- case ASM_CUST_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_basic);
- break;
- case ASM_AUDSTRM_CAL_TYPE:
- size = sizeof(struct audio_cal_type_audstrm);
- break;
- case AFE_TOPOLOGY_CAL_TYPE:
- case AFE_LSM_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_afe_top);
- break;
- case AFE_CUST_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_basic);
- break;
- case AFE_COMMON_RX_CAL_TYPE:
- size = sizeof(struct audio_cal_type_afe);
- break;
- case AFE_COMMON_TX_CAL_TYPE:
- case AFE_LSM_TX_CAL_TYPE:
- size = sizeof(struct audio_cal_type_afe);
- break;
- case AFE_FB_SPKR_PROT_CAL_TYPE:
- size = sizeof(struct audio_cal_type_fb_spk_prot_cfg);
- break;
- case AFE_FB_SPKR_PROT_TH_VI_CAL_TYPE:
- /*
- * Since get and set parameter structures are different in size
- * use the maximum size of get and set parameter structure
- */
- size = max(sizeof(struct audio_cal_type_sp_th_vi_ftm_cfg),
- sizeof(struct audio_cal_type_sp_th_vi_param));
- break;
- case AFE_FB_SPKR_PROT_EX_VI_CAL_TYPE:
- /*
- * Since get and set parameter structures are different in size
- * use the maximum size of get and set parameter structure
- */
- size = max(sizeof(struct audio_cal_type_sp_ex_vi_ftm_cfg),
- sizeof(struct audio_cal_type_sp_ex_vi_param));
- break;
- case AFE_ANC_CAL_TYPE:
- size = 0;
- break;
- case AFE_AANC_CAL_TYPE:
- size = sizeof(struct audio_cal_type_aanc);
- break;
- case AFE_HW_DELAY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_hw_delay);
- break;
- case AFE_SIDETONE_CAL_TYPE:
- size = sizeof(struct audio_cal_type_sidetone);
- break;
- case AFE_SIDETONE_IIR_CAL_TYPE:
- size = sizeof(struct audio_cal_type_sidetone_iir);
- break;
- case LSM_CUST_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_basic);
- break;
- case LSM_TOPOLOGY_CAL_TYPE:
- size = sizeof(struct audio_cal_type_lsm_top);
- break;
- case ULP_LSM_TOPOLOGY_ID_CAL_TYPE:
- size = sizeof(struct audio_cal_type_lsm_top);
- break;
- case LSM_CAL_TYPE:
- size = sizeof(struct audio_cal_type_lsm);
- break;
- case ADM_RTAC_INFO_CAL_TYPE:
- size = 0;
- break;
- case VOICE_RTAC_INFO_CAL_TYPE:
- size = 0;
- break;
- case ADM_RTAC_APR_CAL_TYPE:
- size = 0;
- break;
- case ASM_RTAC_APR_CAL_TYPE:
- size = 0;
- break;
- case VOICE_RTAC_APR_CAL_TYPE:
- size = 0;
- break;
- case MAD_CAL_TYPE:
- size = 0;
- break;
- case ULP_AFE_CAL_TYPE:
- size = sizeof(struct audio_cal_type_afe);
- break;
- case ULP_LSM_CAL_TYPE:
- size = sizeof(struct audio_cal_type_lsm);
- break;
- case AUDIO_CORE_METAINFO_CAL_TYPE:
- size = sizeof(struct audio_cal_type_metainfo);
- break;
- case SRS_TRUMEDIA_CAL_TYPE:
- size = 0;
- break;
- default:
- pr_err("%s:Invalid cal type %d!",
- __func__, cal_type);
- }
- return size;
- }
- int32_t cal_utils_get_cal_type_version(void *cal_type_data)
- {
- struct audio_cal_type_basic *data = NULL;
- data = (struct audio_cal_type_basic *)cal_type_data;
- return data->cal_hdr.version;
- }
- static struct cal_type_data *create_cal_type_data(
- struct cal_type_info *info)
- {
- struct cal_type_data *cal_type = NULL;
- if ((info->reg.cal_type < 0) ||
- (info->reg.cal_type >= MAX_CAL_TYPES)) {
- pr_err("%s: cal type %d is Invalid!\n",
- __func__, info->reg.cal_type);
- goto done;
- }
- if (info->cal_util_callbacks.match_block == NULL) {
- pr_err("%s: cal type %d no method to match blocks!\n",
- __func__, info->reg.cal_type);
- goto done;
- }
- cal_type = kmalloc(sizeof(*cal_type), GFP_KERNEL);
- if (cal_type == NULL)
- goto done;
- INIT_LIST_HEAD(&cal_type->cal_blocks);
- mutex_init(&cal_type->lock);
- memcpy(&cal_type->info, info,
- sizeof(cal_type->info));
- done:
- return cal_type;
- }
- /**
- * cal_utils_create_cal_types
- *
- * @num_cal_types: number of types
- * @cal_type: pointer to the cal types pointer
- * @info: pointer to info
- *
- * Returns 0 on success, EINVAL otherwise
- */
- int cal_utils_create_cal_types(int num_cal_types,
- struct cal_type_data **cal_type,
- struct cal_type_info *info)
- {
- int ret = 0;
- int i;
- pr_debug("%s\n", __func__);
- if (cal_type == NULL) {
- pr_err("%s: cal_type is NULL!\n", __func__);
- ret = -EINVAL;
- goto done;
- } else if (info == NULL) {
- pr_err("%s: info is NULL!\n", __func__);
- ret = -EINVAL;
- goto done;
- } else if ((num_cal_types <= 0) ||
- (num_cal_types > MAX_CAL_TYPES)) {
- pr_err("%s: num_cal_types of %d is Invalid!\n",
- __func__, num_cal_types);
- ret = -EINVAL;
- goto done;
- }
- for (i = 0; i < num_cal_types; i++) {
- if ((info[i].reg.cal_type < 0) ||
- (info[i].reg.cal_type >= MAX_CAL_TYPES)) {
- pr_err("%s: cal type %d at index %d is Invalid!\n",
- __func__, info[i].reg.cal_type, i);
- ret = -EINVAL;
- goto done;
- }
- cal_type[i] = create_cal_type_data(&info[i]);
- if (cal_type[i] == NULL) {
- pr_err("%s: Could not allocate cal_type of index %d!\n",
- __func__, i);
- ret = -EINVAL;
- goto done;
- }
- ret = audio_cal_register(1, &info[i].reg);
- if (ret < 0) {
- pr_err("%s: audio_cal_register failed, ret = %d!\n",
- __func__, ret);
- ret = -EINVAL;
- goto done;
- }
- pr_debug("%s: cal type %d at index %d!\n",
- __func__, info[i].reg.cal_type, i);
- }
- done:
- return ret;
- }
- EXPORT_SYMBOL(cal_utils_create_cal_types);
- static void delete_cal_block(struct cal_block_data *cal_block)
- {
- pr_debug("%s\n", __func__);
- if (cal_block == NULL)
- goto done;
- list_del(&cal_block->list);
- kfree(cal_block->client_info);
- cal_block->client_info = NULL;
- kfree(cal_block->cal_info);
- cal_block->cal_info = NULL;
- if (cal_block->map_data.dma_buf != NULL) {
- msm_audio_ion_free(cal_block->map_data.dma_buf);
- cal_block->map_data.dma_buf = NULL;
- }
- kfree(cal_block);
- done:
- return;
- }
- static void destroy_all_cal_blocks(struct cal_type_data *cal_type)
- {
- int ret = 0;
- struct list_head *ptr, *next;
- struct cal_block_data *cal_block;
- list_for_each_safe(ptr, next,
- &cal_type->cal_blocks) {
- cal_block = list_entry(ptr,
- struct cal_block_data, list);
- ret = unmap_memory(cal_type, cal_block);
- if (ret < 0) {
- pr_err("%s: unmap_memory failed, cal type %d, ret = %d!\n",
- __func__,
- cal_type->info.reg.cal_type,
- ret);
- }
- delete_cal_block(cal_block);
- cal_block = NULL;
- }
- }
- static void destroy_cal_type_data(struct cal_type_data *cal_type)
- {
- if (cal_type == NULL)
- goto done;
- destroy_all_cal_blocks(cal_type);
- list_del(&cal_type->cal_blocks);
- kfree(cal_type);
- done:
- return;
- }
- /**
- * cal_utils_destroy_cal_types -
- * Destroys cal types and deregister from cal info
- *
- * @num_cal_types: number of cal types
- * @cal_type: cal type pointer with cal info
- *
- */
- void cal_utils_destroy_cal_types(int num_cal_types,
- struct cal_type_data **cal_type)
- {
- int i;
- pr_debug("%s\n", __func__);
- if (cal_type == NULL) {
- pr_err("%s: cal_type is NULL!\n", __func__);
- goto done;
- } else if ((num_cal_types <= 0) ||
- (num_cal_types > MAX_CAL_TYPES)) {
- pr_err("%s: num_cal_types of %d is Invalid!\n",
- __func__, num_cal_types);
- goto done;
- }
- for (i = 0; i < num_cal_types; i++) {
- audio_cal_deregister(1, &cal_type[i]->info.reg);
- destroy_cal_type_data(cal_type[i]);
- cal_type[i] = NULL;
- }
- done:
- return;
- }
- EXPORT_SYMBOL(cal_utils_destroy_cal_types);
- /**
- * cal_utils_get_only_cal_block
- *
- * @cal_type: pointer to the cal type
- *
- * Returns cal_block structure
- */
- struct cal_block_data *cal_utils_get_only_cal_block(
- struct cal_type_data *cal_type)
- {
- struct list_head *ptr, *next;
- struct cal_block_data *cal_block = NULL;
- if (cal_type == NULL)
- goto done;
- list_for_each_safe(ptr, next,
- &cal_type->cal_blocks) {
- cal_block = list_entry(ptr,
- struct cal_block_data, list);
- break;
- }
- done:
- return cal_block;
- }
- EXPORT_SYMBOL(cal_utils_get_only_cal_block);
- /**
- * cal_utils_get_only_cal_block
- *
- * @cal_block: pointer to cal block struct
- * @user_data: pointer to user data
- *
- * Returns true on match
- */
- bool cal_utils_match_buf_num(struct cal_block_data *cal_block,
- void *user_data)
- {
- bool ret = false;
- struct audio_cal_type_basic *data = user_data;
- if (cal_block->buffer_number == data->cal_hdr.buffer_number)
- ret = true;
- return ret;
- }
- EXPORT_SYMBOL(cal_utils_match_buf_num);
- static struct cal_block_data *get_matching_cal_block(
- struct cal_type_data *cal_type,
- void *data)
- {
- struct list_head *ptr, *next;
- struct cal_block_data *cal_block = NULL;
- list_for_each_safe(ptr, next,
- &cal_type->cal_blocks) {
- cal_block = list_entry(ptr,
- struct cal_block_data, list);
- if (cal_type->info.cal_util_callbacks.
- match_block(cal_block, data))
- return cal_block;
- }
- return NULL;
- }
- static int cal_block_ion_alloc(struct cal_block_data *cal_block)
- {
- int ret = 0;
- if (cal_block == NULL) {
- pr_err("%s: cal_block is NULL!\n", __func__);
- ret = -EINVAL;
- goto done;
- }
- ret = msm_audio_ion_import(&cal_block->map_data.dma_buf,
- cal_block->map_data.ion_map_handle,
- NULL, 0,
- &cal_block->cal_data.paddr,
- &cal_block->map_data.map_size,
- &cal_block->cal_data.kvaddr);
- if (ret) {
- pr_err("%s: audio ION import failed, rc = %d\n",
- __func__, ret);
- ret = -ENOMEM;
- goto done;
- }
- done:
- return ret;
- }
- static struct cal_block_data *create_cal_block(struct cal_type_data *cal_type,
- struct audio_cal_type_basic *basic_cal,
- size_t client_info_size, void *client_info)
- {
- struct cal_block_data *cal_block = NULL;
- if (cal_type == NULL) {
- pr_err("%s: cal_type is NULL!\n", __func__);
- goto done;
- } else if (basic_cal == NULL) {
- pr_err("%s: basic_cal is NULL!\n", __func__);
- goto done;
- }
- cal_block = kzalloc(sizeof(*cal_block),
- GFP_KERNEL);
- if (cal_block == NULL)
- goto done;
- INIT_LIST_HEAD(&cal_block->list);
- cal_block->map_data.ion_map_handle = basic_cal->cal_data.mem_handle;
- if (basic_cal->cal_data.mem_handle > 0) {
- if (cal_block_ion_alloc(cal_block)) {
- pr_err("%s: cal_block_ion_alloc failed!\n",
- __func__);
- goto err;
- }
- }
- if (client_info_size > 0) {
- cal_block->client_info_size = client_info_size;
- cal_block->client_info = kmalloc(client_info_size, GFP_KERNEL);
- if (cal_block->client_info == NULL) {
- pr_err("%s: could not allocats client_info!\n",
- __func__);
- goto err;
- }
- if (client_info != NULL)
- memcpy(cal_block->client_info, client_info,
- client_info_size);
- }
- cal_block->cal_info = kzalloc(
- get_cal_info_size(cal_type->info.reg.cal_type),
- GFP_KERNEL);
- if (cal_block->cal_info == NULL) {
- pr_err("%s: could not allocats cal_info!\n",
- __func__);
- goto err;
- }
- cal_block->buffer_number = basic_cal->cal_hdr.buffer_number;
- list_add_tail(&cal_block->list, &cal_type->cal_blocks);
- pr_debug("%s: created block for cal type %d, buf num %d, map handle %d, map size %zd paddr 0x%pK!\n",
- __func__, cal_type->info.reg.cal_type,
- cal_block->buffer_number,
- cal_block->map_data.ion_map_handle,
- cal_block->map_data.map_size,
- &cal_block->cal_data.paddr);
- done:
- return cal_block;
- err:
- kfree(cal_block->cal_info);
- cal_block->cal_info = NULL;
- kfree(cal_block->client_info);
- cal_block->client_info = NULL;
- kfree(cal_block);
- cal_block = NULL;
- return cal_block;
- }
- void cal_utils_clear_cal_block_q6maps(int num_cal_types,
- struct cal_type_data **cal_type)
- {
- int i = 0;
- struct list_head *ptr, *next;
- struct cal_block_data *cal_block;
- pr_debug("%s\n", __func__);
- if (cal_type == NULL) {
- pr_err("%s: cal_type is NULL!\n", __func__);
- goto done;
- } else if ((num_cal_types <= 0) ||
- (num_cal_types > MAX_CAL_TYPES)) {
- pr_err("%s: num_cal_types of %d is Invalid!\n",
- __func__, num_cal_types);
- goto done;
- }
- for (; i < num_cal_types; i++) {
- if (cal_type[i] == NULL)
- continue;
- mutex_lock(&cal_type[i]->lock);
- list_for_each_safe(ptr, next,
- &cal_type[i]->cal_blocks) {
- cal_block = list_entry(ptr,
- struct cal_block_data, list);
- cal_block->map_data.q6map_handle = 0;
- }
- mutex_unlock(&cal_type[i]->lock);
- }
- done:
- return;
- }
- static int realloc_memory(struct cal_block_data *cal_block)
- {
- int ret = 0;
- msm_audio_ion_free(cal_block->map_data.dma_buf);
- cal_block->map_data.dma_buf = NULL;
- cal_block->cal_data.size = 0;
- ret = cal_block_ion_alloc(cal_block);
- if (ret < 0)
- pr_err("%s: realloc_memory failed!\n",
- __func__);
- return ret;
- }
- static int map_memory(struct cal_type_data *cal_type,
- struct cal_block_data *cal_block)
- {
- int ret = 0;
- if (cal_type->info.cal_util_callbacks.map_cal != NULL) {
- if ((cal_block->map_data.ion_map_handle < 0) ||
- (cal_block->map_data.map_size <= 0) ||
- (cal_block->map_data.q6map_handle != 0)) {
- goto done;
- }
- pr_debug("%s: cal type %d call map\n",
- __func__, cal_type->info.reg.cal_type);
- ret = cal_type->info.cal_util_callbacks.
- map_cal(cal_type->info.reg.cal_type, cal_block);
- if (ret < 0) {
- pr_err("%s: map_cal failed, cal type %d, ret = %d!\n",
- __func__, cal_type->info.reg.cal_type,
- ret);
- goto done;
- }
- }
- done:
- return ret;
- }
- static int unmap_memory(struct cal_type_data *cal_type,
- struct cal_block_data *cal_block)
- {
- int ret = 0;
- if (cal_type->info.cal_util_callbacks.unmap_cal != NULL) {
- if ((cal_block->map_data.ion_map_handle < 0) ||
- (cal_block->map_data.map_size <= 0) ||
- (cal_block->map_data.q6map_handle == 0)) {
- goto done;
- }
- pr_debug("%s: cal type %d call unmap\n",
- __func__, cal_type->info.reg.cal_type);
- ret = cal_type->info.cal_util_callbacks.
- unmap_cal(cal_type->info.reg.cal_type, cal_block);
- if (ret < 0) {
- pr_err("%s: unmap_cal failed, cal type %d, ret = %d!\n",
- __func__, cal_type->info.reg.cal_type,
- ret);
- goto done;
- }
- }
- done:
- return ret;
- }
- /**
- * cal_utils_alloc_cal
- *
- * @data_size: size of data to allocate
- * @data: data pointer
- * @cal_type: pointer to the cal type
- * @client_info_size: client info size
- * @client_info: pointer to client info
- *
- * Returns 0 on success, appropriate error code otherwise
- */
- int cal_utils_alloc_cal(size_t data_size, void *data,
- struct cal_type_data *cal_type,
- size_t client_info_size, void *client_info)
- {
- int ret = 0;
- struct cal_block_data *cal_block;
- struct audio_cal_type_alloc *alloc_data = data;
- pr_debug("%s\n", __func__);
- if (cal_type == NULL) {
- pr_err("%s: cal_type is NULL!\n",
- __func__);
- ret = -EINVAL;
- goto done;
- }
- if (data_size < sizeof(struct audio_cal_type_alloc)) {
- pr_err("%s: data_size of %zd does not equal alloc struct size of %zd!\n",
- __func__, data_size,
- sizeof(struct audio_cal_type_alloc));
- ret = -EINVAL;
- goto done;
- }
- if ((client_info_size > 0) && (client_info == NULL)) {
- pr_err("%s: User info pointer is NULL but size is %zd!\n",
- __func__, client_info_size);
- ret = -EINVAL;
- goto done;
- }
- if (alloc_data->cal_data.mem_handle < 0) {
- pr_err("%s: mem_handle %d invalid!\n",
- __func__, alloc_data->cal_data.mem_handle);
- ret = -EINVAL;
- goto done;
- }
- mutex_lock(&cal_type->lock);
- cal_block = get_matching_cal_block(cal_type,
- data);
- if (cal_block != NULL) {
- ret = unmap_memory(cal_type, cal_block);
- if (ret < 0)
- goto err;
- ret = realloc_memory(cal_block);
- if (ret < 0)
- goto err;
- } else {
- cal_block = create_cal_block(cal_type,
- (struct audio_cal_type_basic *)alloc_data,
- client_info_size, client_info);
- if (cal_block == NULL) {
- pr_err("%s: create_cal_block failed for %d!\n",
- __func__, alloc_data->cal_data.mem_handle);
- ret = -EINVAL;
- goto err;
- }
- }
- ret = map_memory(cal_type, cal_block);
- if (ret < 0)
- goto err;
- err:
- mutex_unlock(&cal_type->lock);
- done:
- return ret;
- }
- EXPORT_SYMBOL(cal_utils_alloc_cal);
- /**
- * cal_utils_dealloc_cal
- *
- * @data_size: size of data to allocate
- * @data: data pointer
- * @cal_type: pointer to the cal type
- *
- * Returns 0 on success, appropriate error code otherwise
- */
- int cal_utils_dealloc_cal(size_t data_size, void *data,
- struct cal_type_data *cal_type)
- {
- int ret = 0;
- struct cal_block_data *cal_block;
- struct audio_cal_type_dealloc *dealloc_data = data;
- pr_debug("%s\n", __func__);
- if (cal_type == NULL) {
- pr_err("%s: cal_type is NULL!\n",
- __func__);
- ret = -EINVAL;
- goto done;
- }
- if (data_size < sizeof(struct audio_cal_type_dealloc)) {
- pr_err("%s: data_size of %zd does not equal struct size of %zd!\n",
- __func__, data_size,
- sizeof(struct audio_cal_type_dealloc));
- ret = -EINVAL;
- goto done;
- }
- if ((dealloc_data->cal_data.mem_handle == -1) &&
- (dealloc_data->cal_hdr.buffer_number == ALL_CAL_BLOCKS)) {
- destroy_all_cal_blocks(cal_type);
- goto done;
- }
- if (dealloc_data->cal_data.mem_handle < 0) {
- pr_err("%s: mem_handle %d invalid!\n",
- __func__, dealloc_data->cal_data.mem_handle);
- ret = -EINVAL;
- goto done;
- }
- mutex_lock(&cal_type->lock);
- cal_block = get_matching_cal_block(
- cal_type,
- data);
- if (cal_block == NULL) {
- pr_err("%s: allocation does not exist for %d!\n",
- __func__, dealloc_data->cal_data.mem_handle);
- ret = -EINVAL;
- goto err;
- }
- ret = unmap_memory(cal_type, cal_block);
- if (ret < 0)
- goto err;
- delete_cal_block(cal_block);
- err:
- mutex_unlock(&cal_type->lock);
- done:
- return ret;
- }
- EXPORT_SYMBOL(cal_utils_dealloc_cal);
- /**
- * cal_utils_set_cal
- *
- * @data_size: size of data to allocate
- * @data: data pointer
- * @cal_type: pointer to the cal type
- * @client_info_size: client info size
- * @client_info: pointer to client info
- *
- * Returns 0 on success, appropriate error code otherwise
- */
- int cal_utils_set_cal(size_t data_size, void *data,
- struct cal_type_data *cal_type,
- size_t client_info_size, void *client_info)
- {
- int ret = 0;
- struct cal_block_data *cal_block;
- struct audio_cal_type_basic *basic_data = data;
- pr_debug("%s\n", __func__);
- if (cal_type == NULL) {
- pr_err("%s: cal_type is NULL!\n",
- __func__);
- ret = -EINVAL;
- goto done;
- }
- if ((client_info_size > 0) && (client_info == NULL)) {
- pr_err("%s: User info pointer is NULL but size is %zd!\n",
- __func__, client_info_size);
- ret = -EINVAL;
- goto done;
- }
- if ((data_size > get_user_cal_type_size(
- cal_type->info.reg.cal_type)) || (data_size < 0)) {
- pr_err("%s: cal_type %d, data_size of %zd is invalid, expecting %zd!\n",
- __func__, cal_type->info.reg.cal_type, data_size,
- get_user_cal_type_size(cal_type->info.reg.cal_type));
- ret = -EINVAL;
- goto done;
- }
- mutex_lock(&cal_type->lock);
- cal_block = get_matching_cal_block(
- cal_type,
- data);
- if (cal_block == NULL) {
- if (basic_data->cal_data.mem_handle > 0) {
- pr_err("%s: allocation does not exist for %d!\n",
- __func__, basic_data->cal_data.mem_handle);
- ret = -EINVAL;
- goto err;
- } else {
- cal_block = create_cal_block(
- cal_type,
- basic_data,
- client_info_size, client_info);
- if (cal_block == NULL) {
- pr_err("%s: create_cal_block failed for cal type %d!\n",
- __func__,
- cal_type->info.reg.cal_type);
- ret = -EINVAL;
- goto err;
- }
- }
- }
- ret = map_memory(cal_type, cal_block);
- if (ret < 0)
- goto err;
- cal_block->cal_data.size = basic_data->cal_data.cal_size;
- if (client_info_size > 0) {
- memcpy(cal_block->client_info,
- client_info,
- client_info_size);
- }
- memcpy(cal_block->cal_info,
- ((uint8_t *)data + sizeof(struct audio_cal_type_basic)),
- data_size - sizeof(struct audio_cal_type_basic));
- /* reset buffer stale flag */
- cal_block->cal_stale = false;
- err:
- mutex_unlock(&cal_type->lock);
- done:
- return ret;
- }
- EXPORT_SYMBOL(cal_utils_set_cal);
- /**
- * cal_utils_mark_cal_used
- *
- * @cal_block: pointer to cal block
- */
- void cal_utils_mark_cal_used(struct cal_block_data *cal_block)
- {
- if (cal_block)
- cal_block->cal_stale = true;
- }
- EXPORT_SYMBOL(cal_utils_mark_cal_used);
- /**
- * cal_utils_is_cal_stale
- *
- * @cal_block: pointer to cal block
- *
- * Returns true if cal block is stale, false otherwise
- */
- bool cal_utils_is_cal_stale(struct cal_block_data *cal_block)
- {
- if ((cal_block) && (cal_block->cal_stale))
- return true;
- return false;
- }
- EXPORT_SYMBOL(cal_utils_is_cal_stale);
|