Bluetooth: Pan fixes Squashed commit of the following:

commit e91b27c13d16a26500c7138a7e54b054ca44a0cf
Author: Nitin Shivpure <nshivpur@codeaurora.org>
Date:   Thu Jan 2 13:39:36 2014 +0530

    Bluetooth: Fix to avoid A2DP choppiness while disconnecting PAN

    A case where A2DP is going on & PAN is connected. A2DP
    choppiness is observed while disconnecting PAN. Because A2DP
    media task is unable to write data on L2CAP. As BTU task is
    blocked while disconnecting PAN for destroying tap_read_thread,
    tap_read_thread should be destroyed in btif context to avoid
    the A2DP choppiness.

    Change-Id: I0d8c5110743eedf9bce8a41b71a364d0d076ca76
    CRs-Fixed: 593440

commit 2d10f50b168d789e78fbc5122728d41f7db4dc2b
Author: Nitin Shivpure <nshivpur@codeaurora.org>
Date:   Tue Aug 6 21:29:19 2013 +0530

    Bluetooth: Avoid to advertise PANU service records.

    This is special case where remote device(PC) acts as PANU
    & is trying to connect PANU on DUT instead of NAP. Because
    DUT had advertised PANU service records on SDP. As DUT does
    not support PANU-PANU connection, So connection is dropped
    every time. As per spec, It's not mandatory to advertise PANU
    service records, So PANU service record should not be advertised
    on SDP to solve this issue.

    Change-Id: I1f733467335b5e0ca00899cf4380860d233aab33
    CRs-Fixed: 520804
    (cherry picked from commit 0e0811014cc1dc222de0f18b93023b0ff182cc2c)
    (cherry picked from commit 1332c3c8cfbeaf0bae076383aa3be17daac9a1e5)
    (cherry picked from commit 54e0ef1180640b3333330579657737adb15bdb75)

commit 3d495031f5a927a33c7cf66023082ff2ab0f998a
Author: Nitin Shivpure <nshivpur@codeaurora.org>
Date:   Sat Sep 7 14:19:03 2013 +0530

    Bluetooth: Adding support for 5th PANU connection

    DUT(NAP role) can be connected up to maximum 4 remote devices
    (PANU role) as per the current implementation. Adding support
    for 5th PANU connection, So DUT(NAP role) can be connected up
    to maximum 5 Remote devices(PANU role).

    Change-Id: Ic687c56bded40e5e79099ef3c31d40e10b8531c8
    CRs-Fixed: 463828
    (cherry picked from commit 2ff000c8fb06168e9b440834b2a62c1a6d2f8b0f)
    (cherry picked from commit 6641fb84039a1ef3c438712908f5d8895265538d)
    (cherry picked from commit 99547011cdacdb2294cdcc7ff919dc55d8c089d0)

Change-Id: Ic77fde70447499fed55a1ca059e64c10c8189ece
diff --git a/btif/src/btif_pan.c b/btif/src/btif_pan.c
index 32ae984..b44d11a 100644
--- a/btif/src/btif_pan.c
+++ b/btif/src/btif_pan.c
@@ -277,10 +277,10 @@
     btpan_conn_t* conn = btpan_find_conn_addr(bd_addr->address);
     if(conn && conn->handle >= 0)
     {
-        BTA_PanClose(conn->handle);
         /* Inform the application that the disconnect has been initiated successfully */
         btif_transfer_context(btif_in_pan_generic_evt, BTIF_PAN_CB_DISCONNECTING,
                               (char *)bd_addr, sizeof(bt_bdaddr_t), NULL);
+        BTA_PanClose(conn->handle);
         return BT_STATUS_SUCCESS;
     }
     return BT_STATUS_FAIL;
@@ -498,6 +498,29 @@
     return NULL;
 }
 
+static void btpan_close_conn(btpan_conn_t* conn)
+{
+    BTIF_TRACE_DEBUG("btpan_close_conn: %p",conn);
+
+    if (conn && conn->state == PAN_STATE_OPEN)
+    {
+        BTIF_TRACE_DEBUG("btpan_close_conn: PAN_STATE_OPEN");
+
+        conn->state = PAN_STATE_CLOSE;
+        btpan_cb.open_count--;
+
+        if (btpan_cb.open_count == 0)
+        {
+            destroy_tap_read_thread();
+            if (btpan_cb.tap_fd != -1)
+            {
+                btpan_tap_close(btpan_cb.tap_fd);
+                btpan_cb.tap_fd = -1;
+            }
+        }
+    }
+}
+
 static void btpan_cleanup_conn(btpan_conn_t* conn)
 {
     if(conn)
@@ -647,6 +670,7 @@
                 btpan_conn_t* conn = btpan_find_conn_handle(p_data->close.handle);
 
                 ALOGI("%s: event = BTA_PAN_CLOSE_EVT handle %d", __FUNCTION__, p_data->close.handle);
+                btpan_close_conn(conn);
 
                 if(conn && conn->handle >= 0)
                 {