Merge "hardware: gatekeeper: Add delete and delete_all api's" into mnc-dev
diff --git a/include/hardware/sensors.h b/include/hardware/sensors.h
index b8b550f..b368ee6 100644
--- a/include/hardware/sensors.h
+++ b/include/hardware/sensors.h
@@ -108,12 +108,12 @@
      */
     SENSOR_HAL_NORMAL_MODE        = 0,
 
-    /* 
-     * Loopback mode. In this mode, the device shall not source data from the
+    /*
+     * Data Injection mode. In this mode, the device shall not source data from the
      * physical sensors as it would in normal mode. Instead sensor data is
      * injected by the sensor service.
      */
-    SENSOR_HAL_LOOPBACK_MODE      = 0x1
+    SENSOR_HAL_DATA_INJECTION_MODE      = 0x1
 };
 
 /*
@@ -138,7 +138,16 @@
     SENSOR_FLAG_CONTINUOUS_MODE        = 0,    // 0000
     SENSOR_FLAG_ON_CHANGE_MODE         = 0x2,  // 0010
     SENSOR_FLAG_ONE_SHOT_MODE          = 0x4,  // 0100
-    SENSOR_FLAG_SPECIAL_REPORTING_MODE = 0x6   // 0110
+    SENSOR_FLAG_SPECIAL_REPORTING_MODE = 0x6,  // 0110
+
+    /*
+     * Set this flag if the sensor supports data_injection mode and allows data to be injected
+     * from the SensorService. When in data_injection ONLY sensors with this flag set are injected
+     * sensor data and only sensors with this flag set are activated. Eg: Accelerometer and Step
+     * Counter sensors can be set with this flag and SensorService will inject accelerometer data
+     * and read the corresponding step counts.
+     */
+    SENSOR_FLAG_SUPPORTS_DATA_INJECTION = 0x8  // 1000
 };
 
 /*
@@ -148,6 +157,12 @@
 #define REPORTING_MODE_SHIFT             (1)
 
 /*
+ * Mask and shift for data_injection mode sensor flags defined above.
+ */
+#define DATA_INJECTION_MASK              (0x10)
+#define DATA_INJECTION_SHIFT             (4)
+
+/*
  * Sensor type
  *
  * Each sensor has a type which defines what this sensor measures and how
@@ -838,9 +853,9 @@
      *  0 - Normal operation. Default state of the module.
      *  1 - Loopback mode. Data is injected for the the supported
      *      sensors by the sensor service in this mode.
-     * @return 0 on success 
+     * @return 0 on success
      *         -EINVAL if requested mode is not supported
-     *         -EPERM if operation is not allowed 
+     *         -EPERM if operation is not allowed
      */
     int (*set_operation_mode)(unsigned int mode);
 };
@@ -1043,8 +1058,8 @@
     /*
      * Inject a single sensor sample to be to this device.
      * data points to the sensor event to be injected
-     * @return 0 on success 
-     *         -EPERM if operation is not allowed 
+     * @return 0 on success
+     *         -EPERM if operation is not allowed
      *         -EINVAL if sensor event cannot be injected
      */
     int (*inject_sensor_data)(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
diff --git a/modules/usbaudio/audio_hal.c b/modules/usbaudio/audio_hal.c
index 872fa93..bbea5f5 100644
--- a/modules/usbaudio/audio_hal.c
+++ b/modules/usbaudio/audio_hal.c
@@ -80,6 +80,7 @@
     struct audio_stream_out stream;
 
     pthread_mutex_t lock;               /* see note below on mutex acquisition order */
+    pthread_mutex_t pre_lock;           /* acquire before lock to avoid DOS by playback thread */
     bool standby;
 
     struct audio_device *dev;           /* hardware information - only using this for the lock */
@@ -103,7 +104,8 @@
 struct stream_in {
     struct audio_stream_in stream;
 
-    pthread_mutex_t lock; /* see note below on mutex acquisition order */
+    pthread_mutex_t lock;               /* see note below on mutex acquisition order */
+    pthread_mutex_t pre_lock;           /* acquire before lock to avoid DOS by capture thread */
     bool standby;
 
     struct audio_device *dev;           /* hardware information - only using this for the lock */
@@ -126,6 +128,13 @@
 };
 
 /*
+ * NOTE: when multiple mutexes have to be acquired, always take the
+ * stream_in or stream_out mutex first, followed by the audio_device mutex.
+ * stream pre_lock is always acquired before stream lock to prevent starvation of control thread by
+ * higher priority playback or capture thread.
+ */
+
+/*
  * Extract the card and device numbers from the supplied key/value pairs.
  *   kvpairs    A null-terminated string containing the key/value pairs or card and device.
  *              i.e. "card=1;device=42"
@@ -203,6 +212,20 @@
     return result_str;
 }
 
+void lock_input_stream(struct stream_in *in)
+{
+    pthread_mutex_lock(&in->pre_lock);
+    pthread_mutex_lock(&in->lock);
+    pthread_mutex_unlock(&in->pre_lock);
+}
+
+void lock_output_stream(struct stream_out *out)
+{
+    pthread_mutex_lock(&out->pre_lock);
+    pthread_mutex_lock(&out->lock);
+    pthread_mutex_unlock(&out->pre_lock);
+}
+
 /*
  * HAl Functions
  */
@@ -260,16 +283,14 @@
 {
     struct stream_out *out = (struct stream_out *)stream;
 
-    pthread_mutex_lock(&out->dev->lock);
-    pthread_mutex_lock(&out->lock);
-
+    lock_output_stream(out);
     if (!out->standby) {
+        pthread_mutex_lock(&out->dev->lock);
         proxy_close(&out->proxy);
+        pthread_mutex_unlock(&out->dev->lock);
         out->standby = true;
     }
-
     pthread_mutex_unlock(&out->lock);
-    pthread_mutex_unlock(&out->dev->lock);
 
     return 0;
 }
@@ -295,9 +316,9 @@
         return ret_value;
     }
 
+    lock_output_stream(out);
     /* Lock the device because that is where the profile lives */
     pthread_mutex_lock(&out->dev->lock);
-    pthread_mutex_lock(&out->lock);
 
     if (!profile_is_cached_for(out->profile, card, device)) {
         /* cannot read pcm device info if playback is active */
@@ -316,8 +337,8 @@
         }
     }
 
-    pthread_mutex_unlock(&out->lock);
     pthread_mutex_unlock(&out->dev->lock);
+    pthread_mutex_unlock(&out->lock);
 
     return ret_value;
 }
@@ -325,8 +346,8 @@
 static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
 {
     struct stream_out *out = (struct stream_out *)stream;
+    lock_output_stream(out);
     pthread_mutex_lock(&out->dev->lock);
-    pthread_mutex_lock(&out->lock);
 
     char * params_str =  device_get_parameters(out->profile, keys);
 
@@ -360,17 +381,16 @@
     int ret;
     struct stream_out *out = (struct stream_out *)stream;
 
-    pthread_mutex_lock(&out->dev->lock);
-    pthread_mutex_lock(&out->lock);
+    lock_output_stream(out);
     if (out->standby) {
+        pthread_mutex_lock(&out->dev->lock);
         ret = start_output_stream(out);
+        pthread_mutex_unlock(&out->dev->lock);
         if (ret != 0) {
-            pthread_mutex_unlock(&out->dev->lock);
             goto err;
         }
         out->standby = false;
     }
-    pthread_mutex_unlock(&out->dev->lock);
 
     alsa_device_proxy* proxy = &out->proxy;
     const void * write_buff = buffer;
@@ -480,6 +500,9 @@
     out->stream.get_presentation_position = out_get_presentation_position;
     out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
 
+    pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL);
+    pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL);
+
     out->dev = adev;
     pthread_mutex_lock(&adev->lock);
     out->profile = &adev->out_profile;
@@ -639,16 +662,15 @@
 {
     struct stream_in *in = (struct stream_in *)stream;
 
-    pthread_mutex_lock(&in->dev->lock);
-    pthread_mutex_lock(&in->lock);
-
+    lock_input_stream(in);
     if (!in->standby) {
+        pthread_mutex_lock(&in->dev->lock);
         proxy_close(&in->proxy);
+        pthread_mutex_unlock(&in->dev->lock);
         in->standby = true;
     }
 
     pthread_mutex_unlock(&in->lock);
-    pthread_mutex_unlock(&in->dev->lock);
 
     return 0;
 }
@@ -676,8 +698,8 @@
         return ret_value;
     }
 
+    lock_input_stream(in);
     pthread_mutex_lock(&in->dev->lock);
-    pthread_mutex_lock(&in->lock);
 
     if (card >= 0 && device >= 0 && !profile_is_cached_for(in->profile, card, device)) {
         /* cannot read pcm device info if playback is active */
@@ -696,8 +718,8 @@
         }
     }
 
-    pthread_mutex_unlock(&in->lock);
     pthread_mutex_unlock(&in->dev->lock);
+    pthread_mutex_unlock(&in->lock);
 
     return ret_value;
 }
@@ -706,13 +728,13 @@
 {
     struct stream_in *in = (struct stream_in *)stream;
 
+    lock_input_stream(in);
     pthread_mutex_lock(&in->dev->lock);
-    pthread_mutex_lock(&in->lock);
 
     char * params_str =  device_get_parameters(in->profile, keys);
 
-    pthread_mutex_unlock(&in->lock);
     pthread_mutex_unlock(&in->dev->lock);
+    pthread_mutex_unlock(&in->lock);
 
     return params_str;
 }
@@ -750,16 +772,16 @@
 
     struct stream_in * in = (struct stream_in *)stream;
 
-    pthread_mutex_lock(&in->dev->lock);
-    pthread_mutex_lock(&in->lock);
+    lock_input_stream(in);
     if (in->standby) {
-        if (start_input_stream(in) != 0) {
-            pthread_mutex_unlock(&in->dev->lock);
+        pthread_mutex_lock(&in->dev->lock);
+        ret = start_input_stream(in);
+        pthread_mutex_unlock(&in->dev->lock);
+        if (ret != 0) {
             goto err;
         }
         in->standby = false;
     }
-    pthread_mutex_unlock(&in->dev->lock);
 
     alsa_device_profile * profile = in->profile;
 
@@ -858,6 +880,9 @@
     in->stream.read = in_read;
     in->stream.get_input_frames_lost = in_get_input_frames_lost;
 
+    pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL);
+    pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL);
+
     in->dev = (struct audio_device *)dev;
     pthread_mutex_lock(&in->dev->lock);