hal : Fix for stability offload issues.

-Disable and enable the device seperately from
 check_usecases_codec_backend function.
-Acquire device lock for pcm close. This prevents crash during
 headset device switch and fast forward.
-hw free is getting called from compress framework while runtime
 update is going on. The front end list could be cleared during
 runtime update and concurrent access of front end list could
 result a crash.

CRs-Fixed: 568710
Change-Id: Iccc2b9d3c175a5421cfc4d070b0af3906d9080c5
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 74217b7..29997fe 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -358,10 +358,15 @@
             usecase = node_to_item(node, struct audio_usecase, list);
             if (switch_device[usecase->id]) {
                 disable_snd_device(adev, usecase->out_snd_device, false);
-                enable_snd_device(adev, snd_device, false);
             }
         }
 
+        list_for_each(node, &adev->usecase_list) {
+            usecase = node_to_item(node, struct audio_usecase, list);
+            if (switch_device[usecase->id]) {
+                enable_snd_device(adev, snd_device, false);
+            }
+        }
         /* Make sure new snd device is enabled before re-routing the streams */
         audio_route_update_mixer(adev->audio_route);
 
@@ -1198,6 +1203,7 @@
           out->usecase, use_case_table[out->usecase]);
 
     pthread_mutex_lock(&out->lock);
+    pthread_mutex_lock(&adev->lock);
     if (!out->standby) {
         out->standby = true;
         if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
@@ -1214,10 +1220,9 @@
                 out->compr = NULL;
             }
         }
-        pthread_mutex_lock(&adev->lock);
         stop_output_stream(out);
-        pthread_mutex_unlock(&adev->lock);
     }
+    pthread_mutex_unlock(&adev->lock);
     pthread_mutex_unlock(&out->lock);
     ALOGV("%s: exit", __func__);
     return 0;