Bluetooth :Changing the sequence to avoid the deadlock

Due to the variable open and close will be stuck in
loop forever.Moreover change the loop condition to
allow timeout and iteration to 50 since it take
10 secs sometime.

Change-Id: I545bd51a8ddeaf9ff98a9f984f6fa0e6f7915443
CRs-Fixed: 699359
diff --git a/hci/src/userial_mct.c b/hci/src/userial_mct.c
index b48acb2..01f5b25 100644
--- a/hci/src/userial_mct.c
+++ b/hci/src/userial_mct.c
@@ -60,7 +60,7 @@
 #endif
 
 #define MAX_SERIAL_PORT (USERIAL_PORT_3 + 1)
-#define MAX_RETRIAL_CLOSE 10
+#define MAX_RETRIAL_CLOSE 50
 
 enum {
     USERIAL_RX_EXIT,
@@ -280,17 +280,18 @@
 bool userial_open(userial_port_t port)
 {
     int result;
-    userial_state = USERIAL_STATE_OPENING;
 
     USERIALDBG("userial_open(port:%d)", port);
 
     if (userial_running)
     {
+        USERIALDBG("userial_open: userial_running =1");
         /* Userial is open; close it first */
         userial_close();
         utils_delay(50);
     }
 
+    userial_state = USERIAL_STATE_OPENING;
     if (port >= MAX_SERIAL_PORT)
     {
         ALOGE("Port > MAX_SERIAL_PORT");
@@ -422,12 +423,15 @@
 
     USERIALDBG("userial_close");
     userial_close_pending = TRUE;
-    while((userial_state == USERIAL_STATE_OPENING) || (i< MAX_RETRIAL_CLOSE))
+    while((userial_state == USERIAL_STATE_OPENING) && (i< MAX_RETRIAL_CLOSE))
     {
         usleep(200);
         i++;
     }
 
+    if (i == MAX_RETRIAL_CLOSE)
+        USERIALDBG("USERIAL CLOSE : Timeout in creating userial read thread");
+
     if (userial_running)
         send_wakeup_signal(USERIAL_RX_EXIT);