Merge "Bluetooth: Fix to handle invalid VS event during patch download"
diff --git a/libbt-vendor/include/hw_rome.h b/libbt-vendor/include/hw_rome.h
index b6d214c..f2bee16 100644
--- a/libbt-vendor/include/hw_rome.h
+++ b/libbt-vendor/include/hw_rome.h
@@ -106,7 +106,7 @@
 #define EDL_WIP_START_HANDOFF_TO_HOST_EVENT  (0x19)
 #define HCI_VS_GET_ADDON_FEATURES_EVENT      (0x1B)
 #define HCI_VS_GET_BUILD_VER_EVT             (0x05)
-
+#define HCI_VS_STRAY_EVT                (0x17)
 
 /* Status Codes of HCI CMD execution*/
 #define HCI_CMD_SUCCESS                     (0x0)
diff --git a/libbt-vendor/src/hw_rome.c b/libbt-vendor/src/hw_rome.c
index 9876fdc..25182bf 100644
--- a/libbt-vendor/src/hw_rome.c
+++ b/libbt-vendor/src/hw_rome.c
@@ -77,7 +77,8 @@
 unsigned short fw_su_offset =0;
 extern char enable_extldo;
 unsigned char wait_vsc_evt = TRUE;
-
+bool patch_dnld_pending = FALSE;
+int dnld_fd = -1;
 /******************************************************************************
 **  Extern variables
 ******************************************************************************/
@@ -86,7 +87,6 @@
 /*****************************************************************************
 **   Functions
 *****************************************************************************/
-
 int get_vs_hci_event(unsigned char *rsp)
 {
     int err = 0;
@@ -244,6 +244,21 @@
                property_set("persist.bluetooth.a4wp", "true");
             }
             break;
+        case HCI_VS_STRAY_EVT:
+            /* WAR to handle stray Power Apply EVT during patch download */
+            ALOGD("%s: Stray HCI VS EVENT", __FUNCTION__);
+            if (patch_dnld_pending && dnld_fd != -1)
+            {
+                unsigned char rsp[HCI_MAX_EVENT_SIZE];
+                memset(rsp, 0x00, HCI_MAX_EVENT_SIZE);
+                read_vs_hci_event(dnld_fd, rsp, HCI_MAX_EVENT_SIZE);
+            }
+            else
+            {
+                ALOGE("%s: Not a valid status!!!", __FUNCTION__);
+                err = -1;
+            }
+            break;
         default:
             ALOGE("%s: Not a valid status!!!", __FUNCTION__);
             err = -1;
@@ -980,8 +995,10 @@
              }
         }
 
+        patch_dnld_pending = TRUE;
         if((err = rome_tlv_dnld_segment(fd, i, MAX_SIZE_PER_TLV_SEGMENT, wait_cc_evt )) < 0)
             goto error;
+        patch_dnld_pending = FALSE;
     }
 
     if ((rome_ver >= ROME_VER_1_1) && (rome_ver < ROME_VER_3_2) && (gTlv_type == TLV_TYPE_PATCH)) {
@@ -1003,10 +1020,11 @@
            wait_vsc_evt = remain_size ? TRUE: FALSE;
         }
     }
-
+    patch_dnld_pending = TRUE;
     if(remain_size) err =rome_tlv_dnld_segment(fd, i, remain_size, wait_cc_evt);
-
+    patch_dnld_pending = FALSE;
 error:
+    if(patch_dnld_pending) patch_dnld_pending = FALSE;
     return err;
 }
 
@@ -1735,7 +1753,7 @@
 int rome_soc_init(int fd, char *bdaddr)
 {
     int err = -1, size = 0;
-
+    dnld_fd = fd;
     ALOGI(" %s ", __FUNCTION__);
 
     /* If wipower charging is going on in embedded mode then start hand off req */
@@ -1881,5 +1899,6 @@
     }
 
 error:
+    dnld_fd = -1;
     return err;
 }