ASOC: msm: Modify debug messages and conditional checks

Modify debug messages, remove deprecated functions,
proper handling of local pointers, check valid range
of values in conditional checks, check array boundaries
in conditions

Change-Id: I0b20297261727143ec3099599274916404007463
Signed-off-by: Bharath Ramachandramurthy <bramacha@codeaurora.org>
Signed-off-by: Swaminathan Sathappan <Swami@codeaurora.org>
diff --git a/arch/arm/mach-msm/qdsp6v2/amrwb_in.c b/arch/arm/mach-msm/qdsp6v2/amrwb_in.c
index 030d08f..5df976d 100644
--- a/arch/arm/mach-msm/qdsp6v2/amrwb_in.c
+++ b/arch/arm/mach-msm/qdsp6v2/amrwb_in.c
@@ -193,8 +193,8 @@
 	audio = kzalloc(sizeof(struct q6audio_in), GFP_KERNEL);
 
 	if (audio == NULL) {
-		pr_err("%s:session id %d: Could not allocate memory for amrwb"
-			"driver\n", __func__, audio->ac->session);
+		pr_err("%s: Could not allocate memory for amrwb driver\n",
+								__func__);
 		return -ENOMEM;
 	}
 	/* Allocate memory for encoder config param */
@@ -235,8 +235,8 @@
 				(void *)audio);
 
 	if (!audio->ac) {
-		pr_err("%s:session id %d: Could not allocate memory for audio"
-			"client\n", __func__, audio->ac->session);
+		pr_err("%s:audio[%p]: Could not allocate memory for audio"
+			"client\n", __func__, audio);
 		kfree(audio->enc_cfg);
 		kfree(audio);
 		return -ENOMEM;
diff --git a/arch/arm/mach-msm/qdsp6v2/apr.c b/arch/arm/mach-msm/qdsp6v2/apr.c
index 541f62f..b5f071f 100644
--- a/arch/arm/mach-msm/qdsp6v2/apr.c
+++ b/arch/arm/mach-msm/qdsp6v2/apr.c
@@ -255,9 +255,9 @@
 	if (!dest || !svc_name || !svc_fn)
 		return NULL;
 
-	if (!strcmp(dest, "ADSP"))
+	if (!strncmp(dest, "ADSP", 4))
 		dest_id = APR_DEST_QDSP6;
-	else if (!strcmp(dest, "MODEM")) {
+	else if (!strncmp(dest, "MODEM", 5)) {
 		dest_id = APR_DEST_MODEM;
 	} else {
 		pr_err("APR: wrong destination\n");
@@ -286,23 +286,23 @@
 		pr_info("%s: modem Up\n", __func__);
 	}
 
-	if (!strcmp(svc_name, "AFE")) {
+	if (!strncmp(svc_name, "AFE", 3)) {
 		client_id = APR_CLIENT_AUDIO;
 		svc_idx = 0;
 		svc_id = APR_SVC_AFE;
-	} else if (!strcmp(svc_name, "ASM")) {
+	} else if (!strncmp(svc_name, "ASM", 3)) {
 		client_id = APR_CLIENT_AUDIO;
 		svc_idx = 1;
 		svc_id = APR_SVC_ASM;
-	} else if (!strcmp(svc_name, "ADM")) {
+	} else if (!strncmp(svc_name, "ADM", 3)) {
 		client_id = APR_CLIENT_AUDIO;
 		svc_idx = 2;
 		svc_id = APR_SVC_ADM;
-	} else if (!strcmp(svc_name, "CORE")) {
+	} else if (!strncmp(svc_name, "CORE", 4)) {
 		client_id = APR_CLIENT_AUDIO;
 		svc_idx = 3;
 		svc_id = APR_SVC_ADSP_CORE;
-	} else if (!strcmp(svc_name, "TEST")) {
+	} else if (!strncmp(svc_name, "TEST", 4)) {
 		if (dest_id == APR_DEST_QDSP6) {
 			client_id = APR_CLIENT_AUDIO;
 			svc_idx = 4;
@@ -311,19 +311,19 @@
 			svc_idx = 7;
 		}
 		svc_id = APR_SVC_TEST_CLIENT;
-	} else if (!strcmp(svc_name, "VSM")) {
+	} else if (!strncmp(svc_name, "VSM", 3)) {
 		client_id = APR_CLIENT_VOICE;
 		svc_idx = 0;
 		svc_id = APR_SVC_VSM;
-	} else if (!strcmp(svc_name, "VPM")) {
+	} else if (!strncmp(svc_name, "VPM", 3)) {
 		client_id = APR_CLIENT_VOICE;
 		svc_idx = 1;
 		svc_id = APR_SVC_VPM;
-	} else if (!strcmp(svc_name, "MVS")) {
+	} else if (!strncmp(svc_name, "MVS", 3)) {
 		client_id = APR_CLIENT_VOICE;
 		svc_idx = 2;
 		svc_id = APR_SVC_MVS;
-	} else if (!strcmp(svc_name, "MVM")) {
+	} else if (!strncmp(svc_name, "MVM", 3)) {
 		if (dest_id == APR_DEST_MODEM) {
 			client_id = APR_CLIENT_VOICE;
 			svc_idx = 3;
@@ -333,7 +333,7 @@
 			svc_idx = 5;
 			svc_id = APR_SVC_ADSP_MVM;
 		}
-	} else if (!strcmp(svc_name, "CVS")) {
+	} else if (!strncmp(svc_name, "CVS", 3)) {
 		if (dest_id == APR_DEST_MODEM) {
 			client_id = APR_CLIENT_VOICE;
 			svc_idx = 4;
@@ -343,7 +343,7 @@
 			svc_idx = 6;
 			svc_id = APR_SVC_ADSP_CVS;
 		}
-	} else if (!strcmp(svc_name, "CVP")) {
+	} else if (!strncmp(svc_name, "CVP", 3)) {
 		if (dest_id == APR_DEST_MODEM) {
 			client_id = APR_CLIENT_VOICE;
 			svc_idx = 5;
@@ -353,7 +353,7 @@
 			svc_idx = 7;
 			svc_id = APR_SVC_ADSP_CVP;
 		}
-	} else if (!strcmp(svc_name, "SRD")) {
+	} else if (!strncmp(svc_name, "SRD", 3)) {
 		client_id = APR_CLIENT_VOICE;
 		svc_idx = 6;
 		svc_id = APR_SVC_SRD;
@@ -679,16 +679,13 @@
 
 static int __init apr_late_init(void)
 {
-	void *ret;
+	int ret = 0;
 	init_waitqueue_head(&dsp_wait);
 	init_waitqueue_head(&modem_wait);
 	atomic_set(&dsp_state, 1);
 	atomic_set(&modem_state, 1);
-	ret = subsys_notif_register_notifier("modem", &mnb);
-	pr_debug("subsys_register_notifier: ret1 = %p\n", ret);
-	ret = subsys_notif_register_notifier("lpass", &lnb);
-	pr_debug("subsys_register_notifier: ret2 = %p\n", ret);
-
-	return 0;
+	subsys_notif_register_notifier("modem", &mnb);
+	subsys_notif_register_notifier("lpass", &lnb);
+	return ret;
 }
 late_initcall(apr_late_init);
diff --git a/arch/arm/mach-msm/qdsp6v2/rtac.c b/arch/arm/mach-msm/qdsp6v2/rtac.c
index 9823209..a3a14c4 100644
--- a/arch/arm/mach-msm/qdsp6v2/rtac.c
+++ b/arch/arm/mach-msm/qdsp6v2/rtac.c
@@ -442,8 +442,7 @@
 	}
 
 
-	if ((payload_size < 0) ||
-		(payload_size > MAX_PAYLOAD_SIZE)) {
+	if (payload_size > MAX_PAYLOAD_SIZE) {
 
 			pr_err("%s: Invalid payload size = %d\n",
 				__func__, payload_size);
@@ -614,8 +613,7 @@
 		goto done;
 	}
 
-	if ((payload_size < 0) ||
-		(payload_size > MAX_PAYLOAD_SIZE)) {
+	if (payload_size > MAX_PAYLOAD_SIZE) {
 
 			pr_err("%s: Invalid payload size = %d\n",
 				__func__, payload_size);
@@ -627,15 +625,17 @@
 			__func__);
 		goto done;
 	}
-	if (session_id >= AFE_MAX_PORTS) {
+	if (session_id > (SESSION_MAX + 1)) {
 		pr_err("%s: Invalid Session = %d\n", __func__, session_id);
 		goto done;
 	}
 
 	mutex_lock(&rtac_asm_apr_mutex);
-	if (rtac_asm_apr_data[session_id].apr_handle == NULL) {
-		pr_err("%s: APR not initialized\n", __func__);
-		goto err;
+	if (session_id < SESSION_MAX+1) {
+		if (rtac_asm_apr_data[session_id].apr_handle == NULL) {
+			pr_err("%s: APR not initialized\n", __func__);
+			goto err;
+		}
 	}
 
 	/* Set globals for copy of returned payload */
@@ -664,7 +664,8 @@
 	asm_params.opcode = opcode;
 
 	memcpy(rtac_asm_buffer, &asm_params, sizeof(asm_params));
-	atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 1);
+	if (session_id < SESSION_MAX+1)
+		atomic_set(&rtac_asm_apr_data[session_id].cmd_state, 1);
 
 	pr_debug("%s: Sending RTAC command size = %d, session_id=%d\n",
 		__func__, asm_params.pkt_size, session_id);
@@ -723,7 +724,7 @@
 bool rtac_make_voice_callback(u32 mode, uint32_t *payload, u32 payload_size)
 {
 	if ((atomic_read(&rtac_voice_apr_data[mode].cmd_state) != 1) ||
-			(mode < 0) || (mode >= RTAC_VOICE_MODES))
+			(mode >= RTAC_VOICE_MODES))
 		return false;
 
 	pr_debug("%s\n", __func__);
@@ -782,10 +783,8 @@
 		goto done;
 	}
 
-	if ((payload_size < 0) ||
-		(payload_size > MAX_PAYLOAD_SIZE)) {
-
-			pr_err("%s: Invalid payload size = %d\n",
+	if (payload_size > MAX_PAYLOAD_SIZE) {
+		pr_err("%s: Invalid payload size = %d\n",
 				__func__, payload_size);
 		goto done;
 	}
diff --git a/sound/soc/msm/msm-dai-q6.c b/sound/soc/msm/msm-dai-q6.c
index 0430f7b..3d1cea0 100644
--- a/sound/soc/msm/msm-dai-q6.c
+++ b/sound/soc/msm/msm-dai-q6.c
@@ -761,14 +761,14 @@
 
 	dev_dbg(dai->dev, "enter %s, id = %d\n", __func__,
 							dai->id);
-	if (!tx_slot && !rx_slot)
-		return -EINVAL;
 	switch (dai->id) {
 	case SLIMBUS_0_RX:
 		/* channel number to be between 128 and 255. For RX port
 		 * use channel numbers from 138 to 144, for TX port
 		 * use channel numbers from 128 to 137
 		 */
+		if (!rx_slot)
+			return -EINVAL;
 		for (i = 0; i < rx_num; i++) {
 			dai_data->port_config.slim_sch.slave_ch_mapping[i] =
 							rx_slot[i];
@@ -787,6 +787,8 @@
 		 * use channel numbers from 138 to 144, for TX port
 		 * use channel numbers from 128 to 137
 		 */
+		if (!tx_slot)
+			return -EINVAL;
 		for (i = 0; i < tx_num; i++) {
 			dai_data->port_config.slim_sch.slave_ch_mapping[i] =
 							tx_slot[i];
diff --git a/sound/soc/msm/qdsp6/q6adm.c b/sound/soc/msm/qdsp6/q6adm.c
index 7a16a10..a3e1c13 100644
--- a/sound/soc/msm/qdsp6/q6adm.c
+++ b/sound/soc/msm/qdsp6/q6adm.c
@@ -76,7 +76,11 @@
 		index = afe_get_port_index(data->token);
 		pr_debug("%s: Port ID %d, index %d\n", __func__,
 			data->token, index);
-
+		if (index < 0 || index >= AFE_MAX_PORTS) {
+			pr_err("%s: invalid port idx %d token %d\n",
+					__func__, index, data->token);
+			return 0;
+		}
 		if (data->opcode == APR_BASIC_RSP_RESULT) {
 			pr_debug("APR_BASIC_RSP_RESULT\n");
 			switch (payload[0]) {
@@ -139,6 +143,11 @@
 	s32				result = 0;
 	struct adm_set_params_command	adm_params;
 	int index = afe_get_port_index(port_id);
+	if (index < 0 || index >= AFE_MAX_PORTS) {
+		pr_err("%s: invalid port idx %d portid %d\n",
+				__func__, index, port_id);
+		return 0;
+	}
 
 	pr_debug("%s: Port id %d, index %d\n", __func__, port_id, index);
 
@@ -331,8 +340,8 @@
 		open.channel_config = channel_mode & 0x00FF;
 		open.rate  = rate;
 
-		pr_debug("%s: channel_config=%d port_id=%d rate=%d\
-			topology_id=0x%X\n", __func__, open.channel_config,\
+		pr_debug("%s: channel_config=%d port_id=%d rate=%d"
+			"topology_id=0x%X\n", __func__, open.channel_config,\
 			open.endpoint_id1, open.rate,\
 			open.topology_id);
 
@@ -498,6 +507,11 @@
 	int ret = 0, i = 0;
 	/* Assumes port_ids have already been validated during adm_open */
 	int index = afe_get_port_index(copp_id);
+	if (index < 0 || index >= AFE_MAX_PORTS) {
+		pr_err("%s: invalid port idx %d token %d\n",
+					__func__, index, copp_id);
+		return 0;
+	}
 
 	pr_debug("%s: session 0x%x path:%d num_copps:%d port_id[0]:%d\n",
 		 __func__, session_id, path, num_copps, port_id[0]);
@@ -526,7 +540,8 @@
 		pr_debug("%s: port_id[%d]: %d, index: %d\n", __func__, i,
 			 port_id[i], tmp);
 
-		route.session[0].copp_id[i] =
+		if (tmp >= 0 && tmp < AFE_MAX_PORTS)
+			route.session[0].copp_id[i] =
 					atomic_read(&this_adm.copp_id[tmp]);
 	}
 	if (num_copps % 2)