diag: Prevent switching mode for non mdlog process

Switching the mode between USB and PCIE is incorrectly happening
while closing non mdlog process. Patch prevents this issue case
for non mdlog process by checking against the pid of the process
which caused the switch between USB and PCIE.

Change-Id: I609076633fe9dd20287cfc35f0098004bdd11618
Signed-off-by: Manoj Prabhu B <bmanoj@codeaurora.org>
diff --git a/drivers/char/diag/diagchar.h b/drivers/char/diag/diagchar.h
index 36adaa9..7ef332f 100644
--- a/drivers/char/diag/diagchar.h
+++ b/drivers/char/diag/diagchar.h
@@ -656,6 +656,7 @@
 	int usb_connected;
 #endif
 	int pcie_connected;
+	int pcie_switch_pid;
 	struct workqueue_struct *diag_wq;
 	struct work_struct diag_drain_work;
 	struct work_struct update_user_clients;
diff --git a/drivers/char/diag/diagchar_core.c b/drivers/char/diag/diagchar_core.c
index 47da553..e2382a3 100644
--- a/drivers/char/diag/diagchar_core.c
+++ b/drivers/char/diag/diagchar_core.c
@@ -460,15 +460,19 @@
 	session_info = diag_md_session_get_pid(pid);
 	if (!session_info) {
 		mutex_unlock(&driver->md_session_lock);
-		if (driver->pcie_transport_def == DIAG_ROUTE_TO_PCIE)
-			params.req_mode = PCIE_MODE;
-		else
-			params.req_mode = USB_MODE;
-		params.mode_param = 0;
-		params.pd_mask = 0;
-		params.peripheral_mask = DIAG_CON_ALL;
 		mutex_lock(&driver->diagchar_mutex);
-		diag_switch_logging(&params);
+		if (driver->pcie_switch_pid == pid) {
+			if (driver->pcie_transport_def ==
+				DIAG_ROUTE_TO_PCIE)
+				params.req_mode = PCIE_MODE;
+			else
+				params.req_mode = USB_MODE;
+			params.mode_param = 0;
+			params.pd_mask = 0;
+			params.peripheral_mask = DIAG_CON_ALL;
+			diag_switch_logging(&params);
+			driver->pcie_switch_pid = 0;
+		}
 		mutex_unlock(&driver->diagchar_mutex);
 		return;
 	}
@@ -1895,6 +1899,17 @@
 	}
 	driver->logging_mode = new_mode;
 	driver->logging_mask = peripheral_mask;
+	if (((curr_mode == DIAG_PCIE_MODE && new_mode == DIAG_USB_MODE) ||
+		(curr_mode == DIAG_USB_MODE && new_mode == DIAG_PCIE_MODE)) &&
+		!driver->pcie_switch_pid) {
+		/*
+		 * Store the pid of process affecting switch
+		 * from USB to PCIE or vice versa to help
+		 * close only this process while closing
+		 * logging process.
+		 */
+		driver->pcie_switch_pid = current->tgid;
+	}
 	if (new_mode == DIAG_PCIE_MODE) {
 		driver->transport_set = DIAG_ROUTE_TO_PCIE;
 		diagmem_setsize(POOL_TYPE_MUX_APPS, itemsize_pcie_apps,
@@ -4204,6 +4219,7 @@
 	driver->mask_check = 0;
 	driver->in_busy_pktdata = 0;
 	driver->in_busy_dcipktdata = 0;
+	driver->pcie_switch_pid = 0;
 	driver->rsp_buf_ctxt = SET_BUF_CTXT(APPS_DATA, TYPE_CMD, 1);
 	hdlc_data.ctxt = SET_BUF_CTXT(APPS_DATA, TYPE_DATA, 1);
 	hdlc_data.len = 0;