Merge "[gatekeeper] document timeout requirements" into mnc-dev
diff --git a/include/hardware/bluetooth.h b/include/hardware/bluetooth.h
index 893f85d..6f95684 100644
--- a/include/hardware/bluetooth.h
+++ b/include/hardware/bluetooth.h
@@ -536,6 +536,12 @@
       * Success indicates that the VSC command was sent to controller
       */
     int (*read_energy_info)();
+
+    /**
+     * Native support for dumpsys function
+     * Function is synchronous and |fd| is owned by caller.
+     */
+    void (*dump)(int fd);
 } bt_interface_t;
 
 /** TODO: Need to add APIs for Service Discovery, Service authorization and
diff --git a/include/hardware/fingerprint.h b/include/hardware/fingerprint.h
index 552b5d7..a1b81ae 100644
--- a/include/hardware/fingerprint.h
+++ b/include/hardware/fingerprint.h
@@ -177,6 +177,28 @@
     int (*cancel)(struct fingerprint_device *dev);
 
     /*
+     * Enumerate all the fingerprint templates found in the directory set by
+     * set_active_group()
+     * This is a synchronous call. The function takes:
+     * - A pointer to an array of fingerprint_finger_id_t.
+     * - The size of the array provided, in fingerprint_finger_id_t elements.
+     * Max_size is a bi-directional parameter and returns the actual number
+     * of elements copied to the caller supplied array.
+     * In the absence of errors the function returns the total number of templates
+     * in the user directory.
+     * If the caller has no good guess on the size of the array he should call this
+     * function witn *max_size == 0 and use the return value for the array allocation.
+     * The caller of this function has a complete list of the templates when *max_size
+     * is the same as the function return.
+     *
+     * Function return: Total number of fingerprint templates in the current
+                        storage directory.
+     *                 -1 on error.
+     */
+    int (*enumerate)(struct fingerprint_device *dev, fingerprint_finger_id_t *results,
+        uint32_t *max_size);
+
+    /*
      * Fingerprint remove request:
      * deletes a fingerprint template.
      * If the fingerprint id is 0 and the group is 0 then the entire template
@@ -193,12 +215,15 @@
 
     /*
      * Restricts the HAL operation to a set of fingerprints belonging to a
-     * group provided. Gid of 0 signals global operation.
+     * group provided.
+     * The caller must provide a path to a storage location within the user's
+     * data directory.
      *
      * Function return: 0 on success
      *                 -1 if the group does not exist.
      */
-    int (*set_active_group)(struct fingerprint_device *dev, uint32_t gid);
+    int (*set_active_group)(struct fingerprint_device *dev, uint32_t gid,
+                            const char *store_path);
 
     /*
      * Authenticates an operation identifed by operation_id
diff --git a/include/hardware/keymaster1.h b/include/hardware/keymaster1.h
index 4227f57..1d017fd 100644
--- a/include/hardware/keymaster1.h
+++ b/include/hardware/keymaster1.h
@@ -329,43 +329,6 @@
                                                  keymaster_key_characteristics_t** characteristics);
 
     /**
-     * Change a key's authorizations.
-     *
-     * Update the authorizations associated with key_blob to the list specified in new_params, which
-     * must contain the complete set of authorizations desired (hw_enforced and sw_enforced).  Tags
-     * will be added, removed and/or updated only if the appropriate KM_TAG_RESCOPING_ADD and
-     * KM_TAG_RESCOPING_DEL tags exist in the key's authorizations, otherwise
-     * KM_ERROR_INVALID_RESCOPING will be returned and no changes will be made.
-     *
-     * \param[in] dev The keymaster device structure.
-     *
-     * \param[in] new_params The new authorization list to be associated with the key.
-     *
-     * \param[in] new_params_count The number of entries in \p new_params.
-     *
-     * \param[in] key_blob The key to update.
-     *
-     * \param[in] client_id The client ID associated with the key, or NULL if none is associated.
-     *
-     * \param[in] app_data The application data associated with the key, or NULL if none is
-     * associated.
-     *
-     * \param[out] rescoped_key_blob The key blob with the updated authorizations, if successful.
-     * The caller assumes ownership of rescoped_key_blob->key_material and must free() it.
-     *
-     * \param[out] characteristics If not null will contain the new key authorizations, divided into
-     * hw_enforced and sw_enforced lists.  The caller takes ownership and must call
-     * keymaster_free_characteristics() to free.
-     */
-    keymaster_error_t (*rescope)(const struct keymaster1_device* dev,
-                                 const keymaster_key_param_t* new_params, size_t new_params_count,
-                                 const keymaster_key_blob_t* key_blob,
-                                 const keymaster_blob_t* client_id,
-                                 const keymaster_blob_t* app_data,
-                                 keymaster_key_blob_t* rescoped_key_blob,
-                                 keymaster_key_characteristics_t** characteristics);
-
-    /**
      * Imports a key, or key pair, returning a key blob and/or a description of the key.
      *
      * Most key import parameters are defined as keymaster tag/value pairs, provided in "params".
@@ -445,9 +408,8 @@
 
     /**
      * Deletes the key, or key pair, associated with the key blob.  After calling this function it
-     * will be impossible to use the key for any other operations (though rescoped versions may
-     * exist, and if so will be usable).  May be applied to keys from foreign roots of trust (keys
-     * not usable under the current root of trust).
+     * will be impossible to use the key for any other operations.  May be applied to keys from
+     * foreign roots of trust (keys not usable under the current root of trust).
      *
      * This function is optional and should be set to NULL if it is not implemented.
      *
diff --git a/include/hardware/keymaster_defs.h b/include/hardware/keymaster_defs.h
index 9105ee9..8e25e6c 100644
--- a/include/hardware/keymaster_defs.h
+++ b/include/hardware/keymaster_defs.h
@@ -62,14 +62,13 @@
                                              been authenticated.  WARNING: Not recommended. */
     KM_TAG_CALLER_NONCE = KM_BOOL | 8,    /* Allow caller to specify nonce or IV. */
 
-    /* Other hardware-enforced. */
-    KM_TAG_RESCOPING_ADD = KM_ENUM_REP | 101, /* Tags authorized for addition via rescoping. */
-    KM_TAG_RESCOPING_DEL = KM_ENUM_REP | 102, /* Tags authorized for removal via rescoping. */
-    KM_TAG_BLOB_USAGE_REQUIREMENTS = KM_ENUM | 705, /* keymaster_key_blob_usage_requirements_t */
-
     /* Algorithm-specific. */
     KM_TAG_RSA_PUBLIC_EXPONENT = KM_LONG | 200, /* Defaults to 2^16+1 */
 
+    /* Other hardware-enforced. */
+    KM_TAG_BLOB_USAGE_REQUIREMENTS = KM_ENUM | 301, /* keymaster_key_blob_usage_requirements_t */
+    KM_TAG_BOOTLOADER_ONLY = KM_BOOL | 302,         /* Usable only by bootloader */
+
     /*
      * Tags that should be semantically enforced by hardware if possible and will otherwise be
      * enforced by software (keystore).
@@ -279,9 +278,9 @@
  * In the future this list will expand greatly to accommodate asymmetric key import/export.
  */
 typedef enum {
-    KM_KEY_FORMAT_X509 = 0,   /* for public key export */
-    KM_KEY_FORMAT_PKCS8 = 1,  /* for asymmetric key pair import */
-    KM_KEY_FORMAT_RAW = 3,    /* for symmetric key import */
+    KM_KEY_FORMAT_X509 = 0,  /* for public key export */
+    KM_KEY_FORMAT_PKCS8 = 1, /* for asymmetric key pair import */
+    KM_KEY_FORMAT_RAW = 3,   /* for symmetric key import */
 } keymaster_key_format_t;
 
 /**
@@ -335,7 +334,6 @@
     KM_ERROR_UNSUPPORTED_TAG = -39,
     KM_ERROR_INVALID_TAG = -40,
     KM_ERROR_MEMORY_ALLOCATION_FAILED = -41,
-    KM_ERROR_INVALID_RESCOPING = -42,
     KM_ERROR_IMPORT_PARAMETER_MISMATCH = -44,
     KM_ERROR_SECURE_HW_ACCESS_DENIED = -45,
     KM_ERROR_OPERATION_CANCELLED = -46,
@@ -346,7 +344,6 @@
     KM_ERROR_MISSING_NONCE = -51,
     KM_ERROR_INVALID_NONCE = -52,
     KM_ERROR_UNSUPPORTED_CHUNK_LENGTH = -53,
-    KM_ERROR_RESCOPABLE_KEY_NOT_USABLE = -54,
     KM_ERROR_CALLER_NONCE_PROHIBITED = -55,
 
     KM_ERROR_UNIMPLEMENTED = -100,
diff --git a/modules/usbaudio/audio_hal.c b/modules/usbaudio/audio_hal.c
index 3163424..5431476 100644
--- a/modules/usbaudio/audio_hal.c
+++ b/modules/usbaudio/audio_hal.c
@@ -55,6 +55,9 @@
 
 #define DEFAULT_INPUT_BUFFER_SIZE_MS 20
 
+// fixed channel count of 8 limitation (for data processing in AudioFlinger)
+#define FCC_8 8
+
 struct audio_device {
     struct audio_hw_device hw_device;
 
@@ -87,6 +90,8 @@
                                          * the device is not compatible with AudioFlinger
                                          * capabilities, e.g. exposes too many channels or
                                          * too few channels. */
+    audio_channel_mask_t hal_channel_mask;   /* channel mask exposed to AudioFlinger. */
+
     void * conversion_buffer;           /* any conversions are put into here
                                          * they could come from here too if
                                          * there was a previous conversion */
@@ -109,6 +114,8 @@
                                          * the device is not compatible with AudioFlinger
                                          * capabilities, e.g. exposes too many channels or
                                          * too few channels. */
+    audio_channel_mask_t hal_channel_mask;   /* channel mask exposed to AudioFlinger. */
+
     /* We may need to read more data from the device in order to data reduce to 16bit, 4chan */
     void * conversion_buffer;           /* any conversions are put into here
                                          * they could come from here too if
@@ -117,74 +124,6 @@
 };
 
 /*
- * Data Conversions
- */
-/*
- * Convert a buffer of packed (3-byte) PCM24LE samples to PCM16LE samples.
- *   in_buff points to the buffer of PCM24LE samples
- *   num_in_samples size of input buffer in SAMPLES
- *   out_buff points to the buffer to receive converted PCM16LE LE samples.
- * returns
- *   the number of BYTES of output data.
- * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
- * support PCM24_3LE (24-bit, packed).
- * NOTE:
- *   This conversion is safe to do in-place (in_buff == out_buff).
- * TODO Move this to a utilities module.
- */
-static size_t convert_24_3_to_16(const unsigned char * in_buff, size_t num_in_samples,
-                                 short * out_buff)
-{
-    /*
-     * Move from front to back so that the conversion can be done in-place
-     * i.e. in_buff == out_buff
-     */
-    /* we need 2 bytes in the output for every 3 bytes in the input */
-    unsigned char* dst_ptr = (unsigned char*)out_buff;
-    const unsigned char* src_ptr = in_buff;
-    size_t src_smpl_index;
-    for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
-        src_ptr++;               /* lowest-(skip)-byte */
-        *dst_ptr++ = *src_ptr++; /* low-byte */
-        *dst_ptr++ = *src_ptr++; /* high-byte */
-    }
-
-    /* return number of *bytes* generated: */
-    return num_in_samples * 2;
-}
-
-/*
- * Convert a buffer of packed (3-byte) PCM32 samples to PCM16LE samples.
- *   in_buff points to the buffer of PCM32 samples
- *   num_in_samples size of input buffer in SAMPLES
- *   out_buff points to the buffer to receive converted PCM16LE LE samples.
- * returns
- *   the number of BYTES of output data.
- * We are doing this since we *always* present to The Framework as A PCM16LE device, but need to
- * support PCM_FORMAT_S32_LE (32-bit).
- * NOTE:
- *   This conversion is safe to do in-place (in_buff == out_buff).
- * TODO Move this to a utilities module.
- */
-static size_t convert_32_to_16(const int32_t * in_buff, size_t num_in_samples, short * out_buff)
-{
-    /*
-     * Move from front to back so that the conversion can be done in-place
-     * i.e. in_buff == out_buff
-     */
-
-    short * dst_ptr = out_buff;
-    const int32_t* src_ptr = in_buff;
-    size_t src_smpl_index;
-    for (src_smpl_index = 0; src_smpl_index < num_in_samples; src_smpl_index++) {
-        *dst_ptr++ = *src_ptr++ >> 16;
-    }
-
-    /* return number of *bytes* generated: */
-    return num_in_samples * 2;
-}
-
-/*
  * 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"
@@ -296,7 +235,7 @@
 static uint32_t out_get_channels(const struct audio_stream *stream)
 {
     const struct stream_out *out = (const struct stream_out*)stream;
-    return audio_channel_out_mask_from_count(out->hal_channel_count);
+    return out->hal_channel_mask;
 }
 
 static audio_format_t out_get_format(const struct audio_stream *stream)
@@ -582,15 +521,24 @@
     }
 
     /* Channels */
-    unsigned proposed_channel_count = profile_get_default_channel_count(out->profile);
+    unsigned proposed_channel_count = 0;
     if (k_force_channels) {
         proposed_channel_count = k_force_channels;
-    } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
-        proposed_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
+    } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
+        proposed_channel_count =  profile_get_default_channel_count(out->profile);
     }
-    /* we can expose any channel count mask, and emulate internally. */
-    config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
-    out->hal_channel_count = proposed_channel_count;
+    if (proposed_channel_count != 0) {
+        config->channel_mask = audio_channel_out_mask_from_count(proposed_channel_count);
+        if (config->channel_mask == AUDIO_CHANNEL_INVALID)
+            config->channel_mask =
+                    audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
+        out->hal_channel_count = proposed_channel_count;
+    } else {
+        out->hal_channel_count = audio_channel_count_from_out_mask(config->channel_mask);
+    }
+    /* we can expose any channel mask, and emulate internally based on channel count. */
+    out->hal_channel_mask = config->channel_mask;
+
     /* no validity checks are needed as proxy_prepare() forces channel_count to be valid.
      * and we emulate any channel count discrepancies in out_write(). */
     proxy_config.channels = proposed_channel_count;
@@ -664,21 +612,14 @@
 static uint32_t in_get_channels(const struct audio_stream *stream)
 {
     const struct stream_in *in = (const struct stream_in*)stream;
-    return audio_channel_in_mask_from_count(in->hal_channel_count);
+    return in->hal_channel_mask;
 }
 
 static audio_format_t in_get_format(const struct audio_stream *stream)
 {
-    /* TODO Here is the code we need when we support arbitrary input formats
-     * alsa_device_proxy * proxy = ((struct stream_in*)stream)->proxy;
-     * audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
-     * ALOGV("in_get_format() = %d", format);
-     * return format;
-     */
-    /* Input only supports PCM16 */
-    /* TODO When AudioPolicyManager & AudioFlinger supports arbitrary input formats
-       rewrite this to return the ACTUAL channel format (above) */
-    return AUDIO_FORMAT_PCM_16_BIT;
+     alsa_device_proxy *proxy = &((struct stream_in*)stream)->proxy;
+     audio_format_t format = audio_format_from_pcm_format(proxy_get_format(proxy));
+     return format;
 }
 
 static int in_set_format(struct audio_stream *stream, audio_format_t format)
@@ -821,22 +762,13 @@
      * number of bytes in the HAL format (16-bit, stereo).
      */
     num_read_buff_bytes = bytes;
-    int num_device_channels = proxy_get_channel_count(&in->proxy);
-    int num_req_channels = in->hal_channel_count;
+    int num_device_channels = proxy_get_channel_count(&in->proxy); /* what we told Alsa */
+    int num_req_channels = in->hal_channel_count; /* what we told AudioFlinger */
 
     if (num_device_channels != num_req_channels) {
         num_read_buff_bytes = (num_device_channels * num_read_buff_bytes) / num_req_channels;
     }
 
-    enum pcm_format format = proxy_get_format(&in->proxy);
-    if (format == PCM_FORMAT_S24_3LE) {
-        /* 24-bit USB device */
-        num_read_buff_bytes = (3 * num_read_buff_bytes) / 2;
-    } else if (format == PCM_FORMAT_S32_LE) {
-        /* 32-bit USB device */
-        num_read_buff_bytes = num_read_buff_bytes * 2;
-    }
-
     /* Setup/Realloc the conversion buffer (if necessary). */
     if (num_read_buff_bytes != bytes) {
         if (num_read_buff_bytes > in->conversion_buffer_size) {
@@ -850,29 +782,6 @@
 
     ret = proxy_read(&in->proxy, read_buff, num_read_buff_bytes);
     if (ret == 0) {
-        /*
-         * Do any conversions necessary to send the data in the format specified to/by the HAL
-         * (but different from the ALSA format), such as 24bit ->16bit, or 4chan -> 2chan.
-         */
-        if (format != PCM_FORMAT_S16_LE) {
-            /* we need to convert */
-            if (num_device_channels != num_req_channels) {
-                out_buff = read_buff;
-            }
-
-            if (format == PCM_FORMAT_S24_3LE) {
-                num_read_buff_bytes =
-                    convert_24_3_to_16(read_buff, num_read_buff_bytes / 3, out_buff);
-            } else if (format == PCM_FORMAT_S32_LE) {
-                num_read_buff_bytes =
-                    convert_32_to_16(read_buff, num_read_buff_bytes / 4, out_buff);
-            } else {
-                LOG_ALWAYS_FATAL("Unsupported format");
-                num_read_buff_bytes = 0;
-                goto err;
-            }
-        }
-
         if (num_device_channels != num_req_channels) {
             // ALOGV("chans dev:%d req:%d", num_device_channels, num_req_channels);
 
@@ -968,35 +877,39 @@
     }
 
     /* Format */
-    /* until the framework supports format conversion, just take what it asks for
-     * i.e. AUDIO_FORMAT_PCM_16_BIT */
     if (config->format == AUDIO_FORMAT_DEFAULT) {
-        /* just return AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
-         * formats */
-        config->format = AUDIO_FORMAT_PCM_16_BIT;
-        proxy_config.format = PCM_FORMAT_S16_LE;
-    } else if (config->format == AUDIO_FORMAT_PCM_16_BIT) {
-        /* Always accept AUDIO_FORMAT_PCM_16_BIT until the framework supports other input
-         * formats */
-        proxy_config.format = PCM_FORMAT_S16_LE;
+        proxy_config.format = profile_get_default_format(in->profile);
+        config->format = audio_format_from_pcm_format(proxy_config.format);
     } else {
-        /* When the framework support other formats, validate here */
-        config->format = AUDIO_FORMAT_PCM_16_BIT;
-        proxy_config.format = PCM_FORMAT_S16_LE;
-        ret = -EINVAL;
+        enum pcm_format fmt = pcm_format_from_audio_format(config->format);
+        if (profile_is_format_valid(in->profile, fmt)) {
+            proxy_config.format = fmt;
+        } else {
+            proxy_config.format = profile_get_default_format(in->profile);
+            config->format = audio_format_from_pcm_format(proxy_config.format);
+            ret = -EINVAL;
+        }
     }
 
     /* Channels */
-    unsigned proposed_channel_count = profile_get_default_channel_count(in->profile);
+    unsigned proposed_channel_count = 0;
     if (k_force_channels) {
         proposed_channel_count = k_force_channels;
-    } else if (config->channel_mask != AUDIO_CHANNEL_NONE) {
-        proposed_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
+    } else if (config->channel_mask == AUDIO_CHANNEL_NONE) {
+        proposed_channel_count = profile_get_default_channel_count(in->profile);
     }
+    if (proposed_channel_count != 0) {
+        config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
+        if (config->channel_mask == AUDIO_CHANNEL_INVALID)
+            config->channel_mask =
+                    audio_channel_mask_for_index_assignment_from_count(proposed_channel_count);
+        in->hal_channel_count = proposed_channel_count;
+    } else {
+        in->hal_channel_count = audio_channel_count_from_in_mask(config->channel_mask);
+    }
+    /* we can expose any channel mask, and emulate internally based on channel count. */
+    in->hal_channel_mask = config->channel_mask;
 
-    /* we can expose any channel count mask, and emulate internally. */
-    config->channel_mask = audio_channel_in_mask_from_count(proposed_channel_count);
-    in->hal_channel_count = proposed_channel_count;
     proxy_config.channels = profile_get_default_channel_count(in->profile);
     proxy_prepare(&in->proxy, in->profile, &proxy_config);