usb: gadget: fix crash on USB cable disconnection

Crash was observed (9x15) when USB cable was
disconnected. Root cause was NULL pointer and
USB BAM reset due to USB PHY reset. On 9x15 no
reset on disconnect will be performed.

CRs-Fixed: 326999
Change-Id: I006afb8dcd225caf9280dd915f8af3edfaebff5a
Signed-off-by: Ofir Cohen <ofirc@codeaurora.org>
diff --git a/drivers/usb/otg/msm_otg.c b/drivers/usb/otg/msm_otg.c
index d756dd8..4932551 100644
--- a/drivers/usb/otg/msm_otg.c
+++ b/drivers/usb/otg/msm_otg.c
@@ -523,6 +523,18 @@
 	u32 val = 0;
 	u32 ulpi_val = 0;
 
+	/*
+	 * USB PHY and Link reset also reset the USB BAM.
+	 * Thus perform reset operation only once to avoid
+	 * USB BAM reset on other cases e.g. USB cable disconnections.
+	 */
+	if (pdata->disable_reset_on_disconnect) {
+		if (motg->reset_counter)
+			return 0;
+		else
+			motg->reset_counter++;
+	}
+
 	clk_enable(motg->clk);
 	ret = msm_otg_phy_reset(motg);
 	if (ret) {
@@ -2333,6 +2345,9 @@
 		goto free_motg;
 	}
 
+	/* initialize reset counter */
+	motg->reset_counter = 0;
+
 	/* Some targets don't support PHY clock. */
 	motg->phy_reset_clk = clk_get(&pdev->dev, "usb_phy_clk");
 	if (IS_ERR(motg->phy_reset_clk))