am afc55fd4: (-s ours) Extend the audio HAL interface to support get/set master mute: DO NOT MERGE

* commit 'afc55fd404d7d86f0affc9080ccb42aad8745d3a':
  Extend the audio HAL interface to support get/set master mute: DO NOT MERGE
diff --git a/hardware.c b/hardware.c
index 4d5f8fe..1f831cb 100644
--- a/hardware.c
+++ b/hardware.c
@@ -74,7 +74,7 @@
     handle = dlopen(path, RTLD_NOW);
     if (handle == NULL) {
         char const *err_str = dlerror();
-        LOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
+        ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
         status = -EINVAL;
         goto done;
     }
@@ -83,14 +83,14 @@
     const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
     hmi = (struct hw_module_t *)dlsym(handle, sym);
     if (hmi == NULL) {
-        LOGE("load: couldn't find symbol %s", sym);
+        ALOGE("load: couldn't find symbol %s", sym);
         status = -EINVAL;
         goto done;
     }
 
     /* Check that the id matches */
     if (strcmp(id, hmi->id) != 0) {
-        LOGE("load: id=%s != hmi->id=%s", id, hmi->id);
+        ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
         status = -EINVAL;
         goto done;
     }
@@ -108,7 +108,7 @@
             handle = NULL;
         }
     } else {
-        LOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
+        ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
                 id, path, *pHmi, handle);
     }
 
diff --git a/include/hardware/audio.h b/include/hardware/audio.h
index 6770ce0..01d79b2 100644
--- a/include/hardware/audio.h
+++ b/include/hardware/audio.h
@@ -41,6 +41,31 @@
  */
 #define AUDIO_HARDWARE_INTERFACE "audio_hw_if"
 
+
+/* Use version 0.1 to be compatible with first generation of audio hw module with version_major
+ * hardcoded to 1. No audio module API change.
+ */
+#define AUDIO_MODULE_API_VERSION_0_1 HARDWARE_MODULE_API_VERSION(0, 1)
+#define AUDIO_MODULE_API_VERSION_CURRENT AUDIO_MODULE_API_VERSION_0_1
+
+/* First generation of audio devices had version hardcoded to 0. all devices with versions < 1.0
+ * will be considered of first generation API.
+ */
+#define AUDIO_DEVICE_API_VERSION_0_0 HARDWARE_DEVICE_API_VERSION(0, 0)
+#define AUDIO_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
+#define AUDIO_DEVICE_API_VERSION_CURRENT AUDIO_DEVICE_API_VERSION_1_0
+
+/**
+ * List of known audio HAL modules. This is the base name of the audio HAL
+ * library composed of the "audio." prefix, one of the base names below and
+ * a suffix specific to the device.
+ * e.g: audio.primary.goldfish.so or audio.a2dp.default.so
+ */
+
+#define AUDIO_HARDWARE_MODULE_ID_PRIMARY "primary"
+#define AUDIO_HARDWARE_MODULE_ID_A2DP "a2dp"
+#define AUDIO_HARDWARE_MODULE_ID_USB "usb"
+
 /**************************************/
 
 /**
@@ -63,23 +88,50 @@
 #define AUDIO_PARAMETER_VALUE_TTY_HCO "tty_hco"
 #define AUDIO_PARAMETER_VALUE_TTY_FULL "tty_full"
 
+/* A2DP sink address set by framework */
+#define AUDIO_PARAMETER_A2DP_SINK_ADDRESS "a2dp_sink_address"
+
+/* Screen state */
+#define AUDIO_PARAMETER_KEY_SCREEN_STATE "screen_state"
+
 /**
  *  audio stream parameters
  */
 
-#define AUDIO_PARAMETER_STREAM_ROUTING "routing"
-#define AUDIO_PARAMETER_STREAM_FORMAT "format"
-#define AUDIO_PARAMETER_STREAM_CHANNELS "channels"
-#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count"
-#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source"
+#define AUDIO_PARAMETER_STREAM_ROUTING "routing"            // audio_devices_t
+#define AUDIO_PARAMETER_STREAM_FORMAT "format"              // audio_format_t
+#define AUDIO_PARAMETER_STREAM_CHANNELS "channels"          // audio_channel_mask_t
+#define AUDIO_PARAMETER_STREAM_FRAME_COUNT "frame_count"    // size_t
+#define AUDIO_PARAMETER_STREAM_INPUT_SOURCE "input_source"  // audio_source_t
+#define AUDIO_PARAMETER_STREAM_SAMPLING_RATE "sampling_rate" // uint32_t
+
+/* Query supported formats. The response is a '|' separated list of strings from
+ * audio_format_t enum e.g: "sup_formats=AUDIO_FORMAT_PCM_16_BIT" */
+#define AUDIO_PARAMETER_STREAM_SUP_FORMATS "sup_formats"
+/* Query supported channel masks. The response is a '|' separated list of strings from
+ * audio_channel_mask_t enum e.g: "sup_channels=AUDIO_CHANNEL_OUT_STEREO|AUDIO_CHANNEL_OUT_MONO" */
+#define AUDIO_PARAMETER_STREAM_SUP_CHANNELS "sup_channels"
+/* Query supported sampling rates. The response is a '|' separated list of integer values e.g:
+ * "sup_sampling_rates=44100|48000" */
+#define AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES "sup_sampling_rates"
+
 
 /**************************************/
 
+/* common audio stream configuration parameters */
+struct audio_config {
+    uint32_t sample_rate;
+    audio_channel_mask_t channel_mask;
+    audio_format_t  format;
+};
+
+typedef struct audio_config audio_config_t;
+
 /* common audio stream parameters and operations */
 struct audio_stream {
 
     /**
-     * sampling rate is in Hz - eg. 44100
+     * Return the sampling rate in Hz - eg. 44100.
      */
     uint32_t (*get_sample_rate)(const struct audio_stream *stream);
 
@@ -89,28 +141,30 @@
     int (*set_sample_rate)(struct audio_stream *stream, uint32_t rate);
 
     /**
-     * size of output buffer in bytes - eg. 4800
+     * Return size of input/output buffer in bytes for this stream - eg. 4800.
+     * It should be a multiple of the frame size.  See also get_input_buffer_size.
      */
     size_t (*get_buffer_size)(const struct audio_stream *stream);
 
     /**
-     * the channel mask -
+     * Return the channel mask -
      *  e.g. AUDIO_CHANNEL_OUT_STEREO or AUDIO_CHANNEL_IN_STEREO
      */
-    uint32_t (*get_channels)(const struct audio_stream *stream);
+    audio_channel_mask_t (*get_channels)(const struct audio_stream *stream);
 
     /**
-     * audio format - eg. AUDIO_FORMAT_PCM_16_BIT
+     * Return the audio format - e.g. AUDIO_FORMAT_PCM_16_BIT
      */
-    int (*get_format)(const struct audio_stream *stream);
+    audio_format_t (*get_format)(const struct audio_stream *stream);
 
     /* currently unused - use set_parameters with key
      *     AUDIO_PARAMETER_STREAM_FORMAT
      */
-    int (*set_format)(struct audio_stream *stream, int format);
+    int (*set_format)(struct audio_stream *stream, audio_format_t format);
 
     /**
      * Put the audio hardware input/output into standby mode.
+     * Driver should exit from standby mode at the next I/O operation.
      * Returns 0 on success and <0 on failure.
      */
     int (*standby)(struct audio_stream *stream);
@@ -118,7 +172,15 @@
     /** dump the state of the audio input/output device */
     int (*dump)(const struct audio_stream *stream, int fd);
 
+    /** Return the set of device(s) which this stream is connected to */
     audio_devices_t (*get_device)(const struct audio_stream *stream);
+
+    /**
+     * Currently unused - set_device() corresponds to set_parameters() with key
+     * AUDIO_PARAMETER_STREAM_ROUTING for both input and output.
+     * AUDIO_PARAMETER_STREAM_INPUT_SOURCE is an additional information used by
+     * input streams only.
+     */
     int (*set_device)(struct audio_stream *stream, audio_devices_t device);
 
     /**
@@ -138,7 +200,7 @@
 
     /*
      * Returns a pointer to a heap allocated string. The caller is responsible
-     * for freeing the memory for it.
+     * for freeing the memory for it using free().
      */
     char * (*get_parameters)(const struct audio_stream *stream,
                              const char *keys);
@@ -160,7 +222,7 @@
     struct audio_stream common;
 
     /**
-     * return the audio hardware driver latency in milli seconds.
+     * Return the audio hardware driver estimated latency in milliseconds.
      */
     uint32_t (*get_latency)(const struct audio_stream_out *stream);
 
@@ -174,7 +236,10 @@
     int (*set_volume)(struct audio_stream_out *stream, float left, float right);
 
     /**
-     * write audio buffer to driver. Returns number of bytes written
+     * Write audio buffer to driver. Returns number of bytes written, or a
+     * negative status_t. If at least one frame was written successfully prior to the error,
+     * it is suggested that the driver return that successful (short) byte count
+     * and then return an error in the subsequent call.
      */
     ssize_t (*write)(struct audio_stream_out *stream, const void* buffer,
                      size_t bytes);
@@ -186,8 +251,8 @@
                                uint32_t *dsp_frames);
 
     /**
-     * get the local time at which the next write to the audio driver will be
-     * presented
+     * get the local time at which the next write to the audio driver will be presented.
+     * The units are microseconds, where the epoch is decided by the local audio HAL.
      */
     int (*get_next_write_timestamp)(const struct audio_stream_out *stream,
                                     int64_t *timestamp);
@@ -202,7 +267,10 @@
      *  for future use */
     int (*set_gain)(struct audio_stream_in *stream, float gain);
 
-    /** read audio buffer in from audio driver */
+    /** Read audio buffer in from audio driver. Returns number of bytes read, or a
+     *  negative status_t. If at least one frame was read prior to the error,
+     *  read should return that byte count and then return an error in the subsequent call.
+     */
     ssize_t (*read)(struct audio_stream_in *stream, void* buffer,
                     size_t bytes);
 
@@ -223,9 +291,9 @@
 /**
  * return the frame size (number of bytes per sample).
  */
-static inline uint32_t audio_stream_frame_size(struct audio_stream *s)
+static inline size_t audio_stream_frame_size(struct audio_stream *s)
 {
-    int chan_samp_sz;
+    size_t chan_samp_sz;
 
     switch (s->get_format(s)) {
     case AUDIO_FORMAT_PCM_16_BIT:
@@ -284,31 +352,16 @@
      * master volume control.  AudioFlinger will query this value from the
      * primary audio HAL when the service starts and use the value for setting
      * the initial master volume across all HALs.  HALs which do not support
-     * this method may leave it set to NULL.
+     * this method should may leave it set to NULL.
      */
     int (*get_master_volume)(struct audio_hw_device *dev, float *volume);
 
     /**
-     * set the audio mute status for all audio activities.  If any value other
-     * than 0 is returned, the software mixer will emulate this capability.
-     */
-    int (*set_master_mute)(struct audio_hw_device *dev, bool mute);
-
-    /**
-     * Get the current master mute status for the HAL, if the HAL supports
-     * master mute control.  AudioFlinger will query this value from the primary
-     * audio HAL when the service starts and use the value for setting the
-     * initial master mute across all HALs.  HALs which do not support this
-     * method may leave it set to NULL.
-     */
-    int (*get_master_mute)(struct audio_hw_device *dev, bool *mute);
-
-    /**
-     * setMode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
+     * set_mode is called when the audio mode changes. AUDIO_MODE_NORMAL mode
      * is for standard audio playback, AUDIO_MODE_RINGTONE when a ringtone is
      * playing, and AUDIO_MODE_IN_CALL when a call is in progress.
      */
-    int (*set_mode)(struct audio_hw_device *dev, int mode);
+    int (*set_mode)(struct audio_hw_device *dev, audio_mode_t mode);
 
     /* mic mute */
     int (*set_mic_mute)(struct audio_hw_device *dev, bool state);
@@ -319,36 +372,38 @@
 
     /*
      * Returns a pointer to a heap allocated string. The caller is responsible
-     * for freeing the memory for it.
+     * for freeing the memory for it using free().
      */
     char * (*get_parameters)(const struct audio_hw_device *dev,
                              const char *keys);
 
     /* Returns audio input buffer size according to parameters passed or
-     * 0 if one of the parameters is not supported
+     * 0 if one of the parameters is not supported.
+     * See also get_buffer_size which is for a particular stream.
      */
     size_t (*get_input_buffer_size)(const struct audio_hw_device *dev,
-                                    uint32_t sample_rate, int format,
-                                    int channel_count);
+                                    const struct audio_config *config);
 
     /** This method creates and opens the audio hardware output stream */
-    int (*open_output_stream)(struct audio_hw_device *dev, uint32_t devices,
-                              int *format, uint32_t *channels,
-                              uint32_t *sample_rate,
-                              struct audio_stream_out **out);
+    int (*open_output_stream)(struct audio_hw_device *dev,
+                              audio_io_handle_t handle,
+                              audio_devices_t devices,
+                              audio_output_flags_t flags,
+                              struct audio_config *config,
+                              struct audio_stream_out **stream_out);
 
     void (*close_output_stream)(struct audio_hw_device *dev,
-                                struct audio_stream_out* out);
+                                struct audio_stream_out* stream_out);
 
     /** This method creates and opens the audio hardware input stream */
-    int (*open_input_stream)(struct audio_hw_device *dev, uint32_t devices,
-                             int *format, uint32_t *channels,
-                             uint32_t *sample_rate,
-                             audio_in_acoustics_t acoustics,
+    int (*open_input_stream)(struct audio_hw_device *dev,
+                             audio_io_handle_t handle,
+                             audio_devices_t devices,
+                             struct audio_config *config,
                              struct audio_stream_in **stream_in);
 
     void (*close_input_stream)(struct audio_hw_device *dev,
-                               struct audio_stream_in *in);
+                               struct audio_stream_in *stream_in);
 
     /** This method dumps the state of the audio hardware */
     int (*dump)(const struct audio_hw_device *dev, int fd);
diff --git a/include/hardware/audio_effect.h b/include/hardware/audio_effect.h
index 5adab12..4037bbb 100644
--- a/include/hardware/audio_effect.h
+++ b/include/hardware/audio_effect.h
@@ -111,19 +111,19 @@
 //  |                           |           | 1 requires device updates
 //  |                           |           | 2, 4 reserved
 //  +---------------------------+-----------+-----------------------------------
-//  | Sample input mode         | 12..13    | 1 direct: process() function or EFFECT_CMD_CONFIGURE
+//  | Sample input mode         | 12..13    | 1 direct: process() function or EFFECT_CMD_SET_CONFIG
 //  |                           |           |   command must specify a buffer descriptor
 //  |                           |           | 2 provider: process() function uses the
 //  |                           |           |   bufferProvider indicated by the
-//  |                           |           |   EFFECT_CMD_CONFIGURE command to request input.
+//  |                           |           |   EFFECT_CMD_SET_CONFIG command to request input.
 //  |                           |           |   buffers.
 //  |                           |           | 3 both: both input modes are supported
 //  +---------------------------+-----------+-----------------------------------
-//  | Sample output mode        | 14..15    | 1 direct: process() function or EFFECT_CMD_CONFIGURE
+//  | Sample output mode        | 14..15    | 1 direct: process() function or EFFECT_CMD_SET_CONFIG
 //  |                           |           |   command must specify a buffer descriptor
 //  |                           |           | 2 provider: process() function uses the
 //  |                           |           |   bufferProvider indicated by the
-//  |                           |           |   EFFECT_CMD_CONFIGURE command to request output
+//  |                           |           |   EFFECT_CMD_SET_CONFIG command to request output
 //  |                           |           |   buffers.
 //  |                           |           | 3 both: both output modes are supported
 //  +---------------------------+-----------+-----------------------------------
@@ -269,7 +269,7 @@
     //          (count and location) in input buffer descriptor and output processed
     //          samples as specified in output buffer descriptor. If the buffer descriptor
     //          is not specified the function must use either the buffer or the
-    //          buffer provider function installed by the EFFECT_CMD_CONFIGURE command.
+    //          buffer provider function installed by the EFFECT_CMD_SET_CONFIG command.
     //          The effect framework will call the process() function after the EFFECT_CMD_ENABLE
     //          command is received and until the EFFECT_CMD_DISABLE is received. When the engine
     //          receives the EFFECT_CMD_DISABLE command it should turn off the effect gracefully
@@ -284,10 +284,10 @@
     //          self:       handle to the effect interface this function
     //              is called on.
     //          inBuffer:   buffer descriptor indicating where to read samples to process.
-    //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE command.
+    //              If NULL, use the configuration passed by EFFECT_CMD_SET_CONFIG command.
     //
     //          outBuffer:   buffer descriptor indicating where to write processed samples.
-    //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE command.
+    //              If NULL, use the configuration passed by EFFECT_CMD_SET_CONFIG command.
     //
     //    Output:
     //        returned value:    0 successful operation
@@ -369,12 +369,12 @@
     //          self:       handle to the effect interface this function
     //              is called on.
     //          inBuffer:   buffer descriptor indicating where to read samples to process.
-    //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE_REVERSE command.
+    //              If NULL, use the configuration passed by EFFECT_CMD_SET_CONFIG_REVERSE command.
     //
     //          outBuffer:   buffer descriptor indicating where to write processed samples.
-    //              If NULL, use the configuration passed by EFFECT_CMD_CONFIGURE_REVERSE command.
+    //              If NULL, use the configuration passed by EFFECT_CMD_SET_CONFIG_REVERSE command.
     //              If the buffer and buffer provider in the configuration received by
-    //              EFFECT_CMD_CONFIGURE_REVERSE are also NULL, do not return modified reverse
+    //              EFFECT_CMD_SET_CONFIG_REVERSE are also NULL, do not return modified reverse
     //              stream data
     //
     //    Output:
@@ -395,7 +395,7 @@
 //
 enum effect_command_e {
    EFFECT_CMD_INIT,                 // initialize effect engine
-   EFFECT_CMD_CONFIGURE,            // configure effect engine (see effect_config_t)
+   EFFECT_CMD_SET_CONFIG,           // configure effect engine (see effect_config_t)
    EFFECT_CMD_RESET,                // reset effect engine
    EFFECT_CMD_ENABLE,               // enable effect process
    EFFECT_CMD_DISABLE,              // disable effect process
@@ -406,8 +406,13 @@
    EFFECT_CMD_SET_DEVICE,           // set audio device (see audio.h, audio_devices_t)
    EFFECT_CMD_SET_VOLUME,           // set volume
    EFFECT_CMD_SET_AUDIO_MODE,       // set the audio mode (normal, ring, ...)
-   EFFECT_CMD_CONFIGURE_REVERSE,    // configure effect engine reverse stream(see effect_config_t)
+   EFFECT_CMD_SET_CONFIG_REVERSE,   // configure effect engine reverse stream(see effect_config_t)
    EFFECT_CMD_SET_INPUT_DEVICE,     // set capture device (see audio.h, audio_devices_t)
+   EFFECT_CMD_GET_CONFIG,           // read effect engine configuration
+   EFFECT_CMD_GET_CONFIG_REVERSE,   // read configure effect engine reverse stream configuration
+   EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS,// get all supported configurations for a feature.
+   EFFECT_CMD_GET_FEATURE_CONFIG,   // get current feature configuration
+   EFFECT_CMD_SET_FEATURE_CONFIG,   // set current feature configuration
    EFFECT_CMD_FIRST_PROPRIETARY = 0x10000 // first proprietary command code
 };
 
@@ -425,7 +430,7 @@
 //  size: sizeof(int)
 //  data: status
 //==================================================================================================
-// command: EFFECT_CMD_CONFIGURE
+// command: EFFECT_CMD_SET_CONFIG
 //--------------------------------------------------------------------------------------------------
 // description:
 //  Apply new audio parameters configurations for input and output buffers
@@ -579,16 +584,17 @@
 //--------------------------------------------------------------------------------------------------
 // command format:
 //  size: sizeof(uint32_t)
-//  data: audio_mode_e
+//  data: audio_mode_t
 //--------------------------------------------------------------------------------------------------
 // reply format:
 //  size: 0
 //  data: N/A
 //==================================================================================================
-// command: EFFECT_CMD_CONFIGURE_REVERSE
+// command: EFFECT_CMD_SET_CONFIG_REVERSE
 //--------------------------------------------------------------------------------------------------
 // description:
-//  Apply new audio parameters configurations for input and output buffers of reverse stream
+//  Apply new audio parameters configurations for input and output buffers of reverse stream.
+//  An example of reverse stream is the echo reference supplied to an Acoustic Echo Canceler.
 //--------------------------------------------------------------------------------------------------
 // command format:
 //  size: sizeof(effect_config_t)
@@ -614,6 +620,91 @@
 //  size: 0
 //  data: N/A
 //==================================================================================================
+// command: EFFECT_CMD_GET_CONFIG
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Read audio parameters configurations for input and output buffers
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: 0
+//  data: N/A
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: sizeof(effect_config_t)
+//  data: effect_config_t
+//==================================================================================================
+// command: EFFECT_CMD_GET_CONFIG_REVERSE
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Read audio parameters configurations for input and output buffers of reverse stream
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: 0
+//  data: N/A
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: sizeof(effect_config_t)
+//  data: effect_config_t
+//==================================================================================================
+// command: EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Queries for supported configurations for a particular feature (e.g. get the supported
+// combinations of main and auxiliary channels for a noise suppressor).
+// The command parameter is the feature identifier (See effect_feature_e for a list of defined
+// features) followed by the maximum number of configuration descriptor to return.
+// The reply is composed of:
+//  - status (uint32_t):
+//          - 0 if feature is supported
+//          - -ENOSYS if the feature is not supported,
+//          - -ENOMEM if the feature is supported but the total number of supported configurations
+//          exceeds the maximum number indicated by the caller.
+//  - total number of supported configurations (uint32_t)
+//  - an array of configuration descriptors.
+// The actual number of descriptors returned must not exceed the maximum number indicated by
+// the caller.
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: 2 x sizeof(uint32_t)
+//  data: effect_feature_e + maximum number of configurations to return
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: 2 x sizeof(uint32_t) + n x sizeof (<config descriptor>)
+//  data: status + total number of configurations supported + array of n config descriptors
+//==================================================================================================
+// command: EFFECT_CMD_GET_FEATURE_CONFIG
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Retrieves current configuration for a given feature.
+// The reply status is:
+//      - 0 if feature is supported
+//      - -ENOSYS if the feature is not supported,
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: sizeof(uint32_t)
+//  data: effect_feature_e
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: sizeof(uint32_t) + sizeof (<config descriptor>)
+//  data: status + config descriptor
+//==================================================================================================
+// command: EFFECT_CMD_SET_FEATURE_CONFIG
+//--------------------------------------------------------------------------------------------------
+// description:
+//  Sets current configuration for a given feature.
+// The reply status is:
+//      - 0 if feature is supported
+//      - -ENOSYS if the feature is not supported,
+//      - -EINVAL if the configuration is invalid
+//--------------------------------------------------------------------------------------------------
+// command format:
+//  size: sizeof(uint32_t) + sizeof (<config descriptor>)
+//  data: effect_feature_e + config descriptor
+//--------------------------------------------------------------------------------------------------
+// reply format:
+//  size: sizeof(uint32_t)
+//  data: status
+//==================================================================================================
 // command: EFFECT_CMD_FIRST_PROPRIETARY
 //--------------------------------------------------------------------------------------------------
 // description:
@@ -624,7 +715,7 @@
 
 // Audio buffer descriptor used by process(), bufferProvider() functions and buffer_config_t
 // structure. Multi-channel audio is always interleaved. The channel order is from LSB to MSB with
-// regard to the channel mask definition in audio.h, audio_channels_t e.g :
+// regard to the channel mask definition in audio.h, audio_channel_mask_t e.g :
 // Stereo: left, right
 // 5 point 1: front left, front right, front center, low frequency, back left, back right
 // The buffer size is expressed in frame count, a frame being composed of samples for all
@@ -649,7 +740,7 @@
 // with getBuffer() is not needed anymore.
 // The process function should use the buffer provider mechanism to retrieve
 // input or output buffer if the inBuffer or outBuffer passed as argument is NULL
-// and the buffer configuration (buffer_config_t) given by the EFFECT_CMD_CONFIGURE
+// and the buffer configuration (buffer_config_t) given by the EFFECT_CMD_SET_CONFIG
 // command did not specify an audio buffer.
 
 typedef int32_t (* buffer_function_t)(void *cookie, audio_buffer_t *buffer);
@@ -664,11 +755,11 @@
 // The buffer_config_s structure specifies the input or output audio format
 // to be used by the effect engine. It is part of the effect_config_t
 // structure that defines both input and output buffer configurations and is
-// passed by the EFFECT_CMD_CONFIGURE or EFFECT_CMD_CONFIGURE_REVERSE command.
+// passed by the EFFECT_CMD_SET_CONFIG or EFFECT_CMD_SET_CONFIG_REVERSE command.
 typedef struct buffer_config_s {
     audio_buffer_t  buffer;     // buffer for use by process() function if not passed explicitly
     uint32_t   samplingRate;    // sampling rate
-    uint32_t   channels;        // channel mask (see audio_channels_t in audio.h)
+    uint32_t   channels;        // channel mask (see audio_channel_mask_t in audio.h)
     buffer_provider_t bufferProvider;   // buffer provider
     uint8_t    format;          // Audio format  (see see audio_format_t in audio.h)
     uint8_t    accessMode;      // read/write or accumulate in buffer (effect_buffer_access_e)
@@ -684,8 +775,22 @@
 
 };
 
+// feature identifiers for EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS command
+enum effect_feature_e {
+    EFFECT_FEATURE_AUX_CHANNELS, // supports auxiliary channels (e.g. dual mic noise suppressor)
+    EFFECT_FEATURE_CNT
+};
+
+// EFFECT_FEATURE_AUX_CHANNELS feature configuration descriptor. Describe a combination
+// of main and auxiliary channels supported
+typedef struct channel_config_s {
+    audio_channel_mask_t main_channels; // channel mask for main channels
+    audio_channel_mask_t aux_channels;  // channel mask for auxiliary channels
+} channel_config_t;
+
+
 // Values for bit field "mask" in buffer_config_t. If a bit is set, the corresponding field
-// in buffer_config_t must be taken into account when executing the EFFECT_CMD_CONFIGURE command
+// in buffer_config_t must be taken into account when executing the EFFECT_CMD_SET_CONFIG command
 #define EFFECT_CONFIG_BUFFER    0x0001  // buffer field must be taken into account
 #define EFFECT_CONFIG_SMP_RATE  0x0002  // samplingRate field must be taken into account
 #define EFFECT_CONFIG_CHANNELS  0x0004  // channels field must be taken into account
@@ -697,7 +802,7 @@
                            EFFECT_CONFIG_ACC_MODE | EFFECT_CONFIG_PROVIDER)
 
 
-// effect_config_s structure describes the format of the pCmdData argument of EFFECT_CMD_CONFIGURE
+// effect_config_s structure describes the format of the pCmdData argument of EFFECT_CMD_SET_CONFIG
 // command to configure audio parameters and buffers for effect engine input and output.
 typedef struct effect_config_s {
     buffer_config_t   inputCfg;
@@ -840,7 +945,7 @@
     //        *pHandle:         updated with the effect interface handle.
     //
     ////////////////////////////////////////////////////////////////////////////////
-    int32_t (*create_effect)(effect_uuid_t *uuid,
+    int32_t (*create_effect)(const effect_uuid_t *uuid,
                              int32_t sessionId,
                              int32_t ioId,
                              effect_handle_t *pHandle);
@@ -882,7 +987,7 @@
     //        *pDescriptor:     updated with the effect descriptor.
     //
     ////////////////////////////////////////////////////////////////////////////////
-    int32_t (*get_descriptor)(effect_uuid_t *uuid,
+    int32_t (*get_descriptor)(const effect_uuid_t *uuid,
                               effect_descriptor_t *pDescriptor);
 } audio_effect_library_t;
 
diff --git a/include/hardware/audio_policy.h b/include/hardware/audio_policy.h
index fc56e5e..8924dd1 100644
--- a/include/hardware/audio_policy.h
+++ b/include/hardware/audio_policy.h
@@ -91,7 +91,7 @@
                                        audio_policy_dev_state_t state,
                                        const char *device_address);
 
-    /* retreive a device connection status */
+    /* retrieve a device connection status */
     audio_policy_dev_state_t (*get_device_connection_state)(
                                             const struct audio_policy *pol,
                                             audio_devices_t device,
@@ -99,9 +99,9 @@
 
     /* indicate a change in phone state. Valid phones states are defined
      * by audio_mode_t */
-    void (*set_phone_state)(struct audio_policy *pol, int state);
+    void (*set_phone_state)(struct audio_policy *pol, audio_mode_t state);
 
-    /* indicate a change in ringer mode */
+    /* deprecated, never called (was "indicate a change in ringer mode") */
     void (*set_ringer_mode)(struct audio_policy *pol, uint32_t mode,
                             uint32_t mask);
 
@@ -110,7 +110,7 @@
                           audio_policy_force_use_t usage,
                           audio_policy_forced_cfg_t config);
 
-    /* retreive current device category forced for a given usage */
+    /* retrieve current device category forced for a given usage */
     audio_policy_forced_cfg_t (*get_force_use)(const struct audio_policy *pol,
                                                audio_policy_force_use_t usage);
 
@@ -126,14 +126,14 @@
      * Audio routing query functions
      */
 
-    /* request an output appriate for playback of the supplied stream type and
+    /* request an output appropriate for playback of the supplied stream type and
      * parameters */
     audio_io_handle_t (*get_output)(struct audio_policy *pol,
                                     audio_stream_type_t stream,
                                     uint32_t samplingRate,
-                                    uint32_t format,
-                                    uint32_t channels,
-                                    audio_policy_output_flags_t flags);
+                                    audio_format_t format,
+                                    audio_channel_mask_t channelMask,
+                                    audio_output_flags_t flags);
 
     /* indicates to the audio policy manager that the output starts being used
      * by corresponding stream. */
@@ -152,12 +152,12 @@
     /* releases the output. */
     void (*release_output)(struct audio_policy *pol, audio_io_handle_t output);
 
-    /* request an input appriate for record from the supplied device with
+    /* request an input appropriate for record from the supplied device with
      * supplied parameters. */
-    audio_io_handle_t (*get_input)(struct audio_policy *pol, int inputSource,
+    audio_io_handle_t (*get_input)(struct audio_policy *pol, audio_source_t inputSource,
                                    uint32_t samplingRate,
-                                   uint32_t format,
-                                   uint32_t channels,
+                                   audio_format_t format,
+                                   audio_channel_mask_t channelMask,
                                    audio_in_acoustics_t acoustics);
 
     /* indicates to the audio policy manager that the input starts being used */
@@ -174,29 +174,43 @@
      */
 
     /* initialises stream volume conversion parameters by specifying volume
-     * index range. */
+     * index range. The index range for each stream is defined by AudioService. */
     void (*init_stream_volume)(struct audio_policy *pol,
                                audio_stream_type_t stream,
                                int index_min,
                                int index_max);
 
     /* sets the new stream volume at a level corresponding to the supplied
-     * index */
+     * index. The index is within the range specified by init_stream_volume() */
     int (*set_stream_volume_index)(struct audio_policy *pol,
                                    audio_stream_type_t stream,
                                    int index);
 
-    /* retreive current volume index for the specified stream */
+    /* retrieve current volume index for the specified stream */
     int (*get_stream_volume_index)(const struct audio_policy *pol,
                                    audio_stream_type_t stream,
                                    int *index);
 
+    /* sets the new stream volume at a level corresponding to the supplied
+     * index for the specified device.
+     * The index is within the range specified by init_stream_volume() */
+    int (*set_stream_volume_index_for_device)(struct audio_policy *pol,
+                                   audio_stream_type_t stream,
+                                   int index,
+                                   audio_devices_t device);
+
+    /* retrieve current volume index for the specified stream for the specified device */
+    int (*get_stream_volume_index_for_device)(const struct audio_policy *pol,
+                                   audio_stream_type_t stream,
+                                   int *index,
+                                   audio_devices_t device);
+
     /* return the strategy corresponding to a given stream type */
     uint32_t (*get_strategy_for_stream)(const struct audio_policy *pol,
                                         audio_stream_type_t stream);
 
     /* return the enabled output devices for the given stream type */
-    uint32_t (*get_devices_for_stream)(const struct audio_policy *pol,
+    audio_devices_t (*get_devices_for_stream)(const struct audio_policy *pol,
                                        audio_stream_type_t stream);
 
     /* Audio effect management */
@@ -215,13 +229,17 @@
     int (*set_effect_enabled)(struct audio_policy *pol, int id, bool enabled);
 
     bool (*is_stream_active)(const struct audio_policy *pol,
-                             int stream,
+                             audio_stream_type_t stream,
                              uint32_t in_past_ms);
 
     /* dump state */
     int (*dump)(const struct audio_policy *pol, int fd);
 };
 
+/* audio hw module handle used by load_hw_module(), open_output_on_module()
+ * and open_input_on_module() */
+typedef int audio_module_handle_t;
+
 struct audio_policy_service_ops {
     /*
      * Audio output Control functions
@@ -240,12 +258,12 @@
      * suitable or not and act accordingly.
      */
     audio_io_handle_t (*open_output)(void *service,
-                                     uint32_t *pDevices,
+                                     audio_devices_t *pDevices,
                                      uint32_t *pSamplingRate,
-                                     uint32_t *pFormat,
-                                     uint32_t *pChannels,
+                                     audio_format_t *pFormat,
+                                     audio_channel_mask_t *pChannelMask,
                                      uint32_t *pLatencyMs,
-                                     audio_policy_output_flags_t flags);
+                                     audio_output_flags_t flags);
 
     /* creates a special output that is duplicated to the two outputs passed as
      * arguments. The duplication is performed by
@@ -275,11 +293,11 @@
 
     /* opens an audio input */
     audio_io_handle_t (*open_input)(void *service,
-                                    uint32_t *pDevices,
+                                    audio_devices_t *pDevices,
                                     uint32_t *pSamplingRate,
-                                    uint32_t *pFormat,
-                                    uint32_t *pChannels,
-                                    uint32_t acoustics);
+                                    audio_format_t *pFormat,
+                                    audio_channel_mask_t *pChannelMask,
+                                    audio_in_acoustics_t acoustics);
 
     /* closes an audio input */
     int (*close_input)(void *service, audio_io_handle_t input);
@@ -315,7 +333,7 @@
      * audio hardware interface to audio policy manager.
      *
      * Returns a pointer to a heap allocated string. The caller is responsible
-     * for freeing the memory for it.
+     * for freeing the memory for it using free().
      */
 
     char * (*get_parameters)(void *service, audio_io_handle_t io_handle,
@@ -341,6 +359,41 @@
                         int session,
                         audio_io_handle_t src_output,
                         audio_io_handle_t dst_output);
+
+    /* loads an audio hw module.
+     *
+     * The module name passed is the base name of the HW module library, e.g "primary" or "a2dp".
+     * The function returns a handle on the module that will be used to specify a particular
+     * module when calling open_output_on_module() or open_input_on_module()
+     */
+    audio_module_handle_t (*load_hw_module)(void *service,
+                                              const char *name);
+
+    /* Opens an audio output on a particular HW module.
+     *
+     * Same as open_output() but specifying a specific HW module on which the output must be opened.
+     */
+    audio_io_handle_t (*open_output_on_module)(void *service,
+                                     audio_module_handle_t module,
+                                     audio_devices_t *pDevices,
+                                     uint32_t *pSamplingRate,
+                                     audio_format_t *pFormat,
+                                     audio_channel_mask_t *pChannelMask,
+                                     uint32_t *pLatencyMs,
+                                     audio_output_flags_t flags);
+
+    /* Opens an audio input on a particular HW module.
+     *
+     * Same as open_input() but specifying a specific HW module on which the input must be opened.
+     * Also removed deprecated acoustics parameter
+     */
+    audio_io_handle_t (*open_input_on_module)(void *service,
+                                    audio_module_handle_t module,
+                                    audio_devices_t *pDevices,
+                                    uint32_t *pSamplingRate,
+                                    audio_format_t *pFormat,
+                                    audio_channel_mask_t *pChannelMask);
+
 };
 
 /**********************************************************************/
diff --git a/include/hardware/camera.h b/include/hardware/camera.h
index 4d25023..b7182f8 100644
--- a/include/hardware/camera.h
+++ b/include/hardware/camera.h
@@ -14,53 +14,29 @@
  * limitations under the License.
  */
 
-// FIXME: add well-defined names for cameras
-
 #ifndef ANDROID_INCLUDE_CAMERA_H
 #define ANDROID_INCLUDE_CAMERA_H
 
-#include <stdint.h>
-#include <sys/cdefs.h>
-#include <sys/types.h>
-#include <cutils/native_handle.h>
-#include <system/camera.h>
-#include <hardware/hardware.h>
-#include <hardware/gralloc.h>
-
-__BEGIN_DECLS
+#include "camera_common.h"
 
 /**
- * The id of this module
+ * Camera device HAL, initial version [ CAMERA_DEVICE_API_VERSION_1_0 ]
+ *
+ * Supports the android.hardware.Camera API.
+ *
+ * Camera devices that support this version of the HAL must return a value in
+ * the range HARDWARE_DEVICE_API_VERSION(0,0)-(1,FF) in
+ * camera_device_t.common.version. CAMERA_DEVICE_API_VERSION_1_0 is the
+ * recommended value.
+ *
+ * Camera modules that implement version 2.0 or higher of camera_module_t must
+ * also return the value of camera_device_t.common.version in
+ * camera_info_t.device_version.
+ *
+ * See camera_common.h for more details.
  */
-#define CAMERA_HARDWARE_MODULE_ID "camera"
 
-struct camera_info {
-    /**
-     * The direction that the camera faces to. It should be CAMERA_FACING_BACK
-     * or CAMERA_FACING_FRONT.
-     */
-    int facing;
-
-    /**
-     * The orientation of the camera image. The value is the angle that the
-     * camera image needs to be rotated clockwise so it shows correctly on the
-     * display in its natural orientation. It should be 0, 90, 180, or 270.
-     *
-     * For example, suppose a device has a naturally tall screen. The
-     * back-facing camera sensor is mounted in landscape. You are looking at
-     * the screen. If the top side of the camera sensor is aligned with the
-     * right edge of the screen in natural orientation, the value should be
-     * 90. If the top side of a front-facing camera sensor is aligned with the
-     * right of the screen, the value should be 270.
-     */
-    int orientation;
-};
-
-typedef struct camera_module {
-    hw_module_t common;
-    int (*get_number_of_cameras)(void);
-    int (*get_camera_info)(int camera_id, struct camera_info *info);
-} camera_module_t;
+__BEGIN_DECLS
 
 struct camera_memory;
 typedef void (*camera_release_memory)(struct camera_memory *mem);
@@ -109,6 +85,11 @@
                 int *count);
     int (*lock_buffer)(struct preview_stream_ops* w,
                 buffer_handle_t* buffer);
+    // Timestamps are measured in nanoseconds, and must be comparable
+    // and monotonically increasing between two frames in the same
+    // preview stream. They do not need to be comparable between
+    // consecutive or parallel preview streams, cameras, or app runs.
+    int (*set_timestamp)(struct preview_stream_ops *w, int64_t timestamp);
 } preview_stream_ops_t;
 
 struct camera_device;
@@ -299,6 +280,11 @@
 } camera_device_ops_t;
 
 typedef struct camera_device {
+    /**
+     * camera_device.common.version must be in the range
+     * HARDWARE_DEVICE_API_VERSION(0,0)-(1,FF). CAMERA_DEVICE_API_VERSION_1_0 is
+     * recommended.
+     */
     hw_device_t common;
     camera_device_ops_t *ops;
     void *priv;
@@ -306,4 +292,4 @@
 
 __END_DECLS
 
-#endif /* ANDROID_INCLUDE_CAMERA_H */
+#endif /* #ifdef ANDROID_INCLUDE_CAMERA_H */
diff --git a/include/hardware/camera2.h b/include/hardware/camera2.h
new file mode 100644
index 0000000..f3e4ba4
--- /dev/null
+++ b/include/hardware/camera2.h
@@ -0,0 +1,673 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_INCLUDE_CAMERA2_H
+#define ANDROID_INCLUDE_CAMERA2_H
+
+#include "camera_common.h"
+#include "system/camera_metadata.h"
+
+/**
+ * Camera device HAL 2.0 [ CAMERA_DEVICE_API_VERSION_2_0 ]
+ *
+ * EXPERIMENTAL.
+ *
+ * Supports both the android.hardware.ProCamera and
+ * android.hardware.Camera APIs.
+ *
+ * Camera devices that support this version of the HAL must return
+ * CAMERA_DEVICE_API_VERSION_2_0 in camera_device_t.common.version and in
+ * camera_info_t.device_version (from camera_module_t.get_camera_info).
+ *
+ * Camera modules that may contain version 2.0 devices must implement at least
+ * version 2.0 of the camera module interface (as defined by
+ * camera_module_t.common.module_api_version).
+ *
+ * See camera_common.h for more versioning details.
+ *
+ */
+
+__BEGIN_DECLS
+
+struct camera2_device;
+
+/**********************************************************************
+ *
+ * Input/output stream buffer queue interface definitions
+ *
+ */
+
+/**
+ * Output image stream queue interface. A set of these methods is provided to
+ * the HAL device in allocate_stream(), and are used to interact with the
+ * gralloc buffer queue for that stream. They may not be called until after
+ * allocate_stream returns.
+ */
+typedef struct camera2_stream_ops {
+    /**
+     * Get a buffer to fill from the queue. The size and format of the buffer
+     * are fixed for a given stream (defined in allocate_stream), and the stride
+     * should be queried from the platform gralloc module. The gralloc buffer
+     * will have been allocated based on the usage flags provided by
+     * allocate_stream, and will be locked for use.
+     */
+    int (*dequeue_buffer)(const struct camera2_stream_ops* w,
+            buffer_handle_t** buffer);
+
+    /**
+     * Push a filled buffer to the stream to be used by the consumer.
+     *
+     * The timestamp represents the time at start of exposure of the first row
+     * of the image; it must be from a monotonic clock, and is measured in
+     * nanoseconds. The timestamps do not need to be comparable between
+     * different cameras, or consecutive instances of the same camera. However,
+     * they must be comparable between streams from the same camera. If one
+     * capture produces buffers for multiple streams, each stream must have the
+     * same timestamp for that buffer, and that timestamp must match the
+     * timestamp in the output frame metadata.
+     */
+    int (*enqueue_buffer)(const struct camera2_stream_ops* w,
+            int64_t timestamp,
+            buffer_handle_t* buffer);
+    /**
+     * Return a buffer to the queue without marking it as filled.
+     */
+    int (*cancel_buffer)(const struct camera2_stream_ops* w,
+            buffer_handle_t* buffer);
+    /**
+     * Set the crop window for subsequently enqueued buffers. The parameters are
+     * measured in pixels relative to the buffer width and height.
+     */
+    int (*set_crop)(const struct camera2_stream_ops *w,
+            int left, int top, int right, int bottom);
+
+} camera2_stream_ops_t;
+
+/**
+ * Special pixel format value used to indicate that the framework does not care
+ * what exact pixel format is to be used for an output stream. The device HAL is
+ * free to select any pixel format, platform-specific and otherwise, and this
+ * opaque value will be passed on to the platform gralloc module when buffers
+ * need to be allocated for the stream.
+ */
+enum {
+    CAMERA2_HAL_PIXEL_FORMAT_OPAQUE = -1
+};
+
+/**
+ * Input reprocess stream queue management. A set of these methods is provided
+ * to the HAL device in allocate_reprocess_stream(); they are used to interact
+ * with the reprocess stream's input gralloc buffer queue.
+ */
+typedef struct camera2_stream_in_ops {
+    /**
+     * Get the next buffer of image data to reprocess. The width, height, and
+     * format of the buffer is fixed in allocate_reprocess_stream(), and the
+     * stride and other details should be queried from the platform gralloc
+     * module as needed. The buffer will already be locked for use.
+     */
+    int (*acquire_buffer)(const struct camera2_stream_in_ops *w,
+            buffer_handle_t** buffer);
+    /**
+     * Return a used buffer to the buffer queue for reuse.
+     */
+    int (*release_buffer)(const struct camera2_stream_in_ops *w,
+            buffer_handle_t* buffer);
+
+} camera2_stream_in_ops_t;
+
+/**********************************************************************
+ *
+ * Metadata queue management, used for requests sent to HAL module, and for
+ * frames produced by the HAL.
+ *
+ */
+
+enum {
+    CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS = -1
+};
+
+/**
+ * Request input queue protocol:
+ *
+ * The framework holds the queue and its contents. At start, the queue is empty.
+ *
+ * 1. When the first metadata buffer is placed into the queue, the framework
+ *    signals the device by calling notify_request_queue_not_empty().
+ *
+ * 2. After receiving notify_request_queue_not_empty, the device must call
+ *    dequeue() once it's ready to handle the next buffer.
+ *
+ * 3. Once the device has processed a buffer, and is ready for the next buffer,
+ *    it must call dequeue() again instead of waiting for a notification. If
+ *    there are no more buffers available, dequeue() will return NULL. After
+ *    this point, when a buffer becomes available, the framework must call
+ *    notify_request_queue_not_empty() again. If the device receives a NULL
+ *    return from dequeue, it does not need to query the queue again until a
+ *    notify_request_queue_not_empty() call is received from the source.
+ *
+ * 4. If the device calls buffer_count() and receives 0, this does not mean that
+ *    the framework will provide a notify_request_queue_not_empty() call. The
+ *    framework will only provide such a notification after the device has
+ *    received a NULL from dequeue, or on initial startup.
+ *
+ * 5. The dequeue() call in response to notify_request_queue_not_empty() may be
+ *    on the same thread as the notify_request_queue_not_empty() call, and may
+ *    be performed from within the notify call.
+ *
+ * 6. All dequeued request buffers must be returned to the framework by calling
+ *    free_request, including when errors occur, a device flush is requested, or
+ *    when the device is shutting down.
+ */
+typedef struct camera2_request_queue_src_ops {
+    /**
+     * Get the count of request buffers pending in the queue. May return
+     * CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS if a repeating request (stream
+     * request) is currently configured. Calling this method has no effect on
+     * whether the notify_request_queue_not_empty() method will be called by the
+     * framework.
+     */
+    int (*request_count)(const struct camera2_request_queue_src_ops *q);
+
+    /**
+     * Get a metadata buffer from the framework. Returns OK if there is no
+     * error. If the queue is empty, returns NULL in buffer. In that case, the
+     * device must wait for a notify_request_queue_not_empty() message before
+     * attempting to dequeue again. Buffers obtained in this way must be
+     * returned to the framework with free_request().
+     */
+    int (*dequeue_request)(const struct camera2_request_queue_src_ops *q,
+            camera_metadata_t **buffer);
+    /**
+     * Return a metadata buffer to the framework once it has been used, or if
+     * an error or shutdown occurs.
+     */
+    int (*free_request)(const struct camera2_request_queue_src_ops *q,
+            camera_metadata_t *old_buffer);
+
+} camera2_request_queue_src_ops_t;
+
+/**
+ * Frame output queue protocol:
+ *
+ * The framework holds the queue and its contents. At start, the queue is empty.
+ *
+ * 1. When the device is ready to fill an output metadata frame, it must dequeue
+ *    a metadata buffer of the required size.
+ *
+ * 2. It should then fill the metadata buffer, and place it on the frame queue
+ *    using enqueue_frame. The framework takes ownership of the frame.
+ *
+ * 3. In case of an error, a request to flush the pipeline, or shutdown, the
+ *    device must return any affected dequeued frames to the framework by
+ *    calling cancel_frame.
+ */
+typedef struct camera2_frame_queue_dst_ops {
+    /**
+     * Get an empty metadata buffer to fill from the framework. The new metadata
+     * buffer will have room for entries number of metadata entries, plus
+     * data_bytes worth of extra storage. Frames dequeued here must be returned
+     * to the framework with either cancel_frame or enqueue_frame.
+     */
+    int (*dequeue_frame)(const struct camera2_frame_queue_dst_ops *q,
+            size_t entries, size_t data_bytes,
+            camera_metadata_t **buffer);
+
+    /**
+     * Return a dequeued metadata buffer to the framework for reuse; do not mark it as
+     * filled. Use when encountering errors, or flushing the internal request queue.
+     */
+    int (*cancel_frame)(const struct camera2_frame_queue_dst_ops *q,
+            camera_metadata_t *buffer);
+
+    /**
+     * Place a completed metadata frame on the frame output queue.
+     */
+    int (*enqueue_frame)(const struct camera2_frame_queue_dst_ops *q,
+            camera_metadata_t *buffer);
+
+} camera2_frame_queue_dst_ops_t;
+
+/**********************************************************************
+ *
+ * Notification callback and message definition, and trigger definitions
+ *
+ */
+
+/**
+ * Asynchronous notification callback from the HAL, fired for various
+ * reasons. Only for information independent of frame capture, or that require
+ * specific timing. The user pointer must be the same one that was passed to the
+ * device in set_notify_callback().
+ */
+typedef void (*camera2_notify_callback)(int32_t msg_type,
+        int32_t ext1,
+        int32_t ext2,
+        int32_t ext3,
+        void *user);
+
+/**
+ * Possible message types for camera2_notify_callback
+ */
+enum {
+    /**
+     * An error has occurred. Argument ext1 contains the error code, and
+     * ext2 and ext3 contain any error-specific information.
+     */
+    CAMERA2_MSG_ERROR   = 0x0001,
+    /**
+     * The exposure of a given request has begun. Argument ext1 contains the
+     * frame number, and ext2 and ext3 contain the low-order and high-order
+     * bytes of the timestamp for when exposure began.
+     * (timestamp = (ext3 << 32 | ext2))
+     */
+    CAMERA2_MSG_SHUTTER = 0x0010,
+    /**
+     * The autofocus routine has changed state. Argument ext1 contains the new
+     * state; the values are the same as those for the metadata field
+     * android.control.afState. Ext2 contains the latest value passed to
+     * trigger_action(CAMERA2_TRIGGER_AUTOFOCUS), or 0 if that method has not
+     * been called.
+     */
+    CAMERA2_MSG_AUTOFOCUS = 0x0020,
+    /**
+     * The autoexposure routine has changed state. Argument ext1 contains the
+     * new state; the values are the same as those for the metadata field
+     * android.control.aeState. Ext2 containst the latest value passed to
+     * trigger_action(CAMERA2_TRIGGER_PRECAPTURE_METERING), or 0 if that method
+     * has not been called.
+     */
+    CAMERA2_MSG_AUTOEXPOSURE = 0x0021,
+    /**
+     * The auto-whitebalance routine has changed state. Argument ext1 contains
+     * the new state; the values are the same as those for the metadata field
+     * android.control.awbState.
+     */
+    CAMERA2_MSG_AUTOWB = 0x0022
+};
+
+/**
+ * Error codes for CAMERA_MSG_ERROR
+ */
+enum {
+    /**
+     * A serious failure occured. Camera device may not work without reboot, and
+     * no further frames or buffer streams will be produced by the
+     * device. Device should be treated as closed.
+     */
+    CAMERA2_MSG_ERROR_HARDWARE = 0x0001,
+    /**
+     * A serious failure occured. No further frames or buffer streams will be
+     * produced by the device. Device should be treated as closed. The client
+     * must reopen the device to use it again.
+     */
+    CAMERA2_MSG_ERROR_DEVICE,
+    /**
+     * An error has occurred in processing a request. No output (metadata or
+     * buffers) will be produced for this request. ext2 contains the frame
+     * number of the request. Subsequent requests are unaffected, and the device
+     * remains operational.
+     */
+    CAMERA2_MSG_ERROR_REQUEST,
+    /**
+     * An error has occurred in producing an output frame metadata buffer for a
+     * request, but image buffers for it will still be available. Subsequent
+     * requests are unaffected, and the device remains operational. ext2
+     * contains the frame number of the request.
+     */
+    CAMERA2_MSG_ERROR_FRAME,
+    /**
+     * An error has occurred in placing an output buffer into a stream for a
+     * request. The frame metadata and other buffers may still be
+     * available. Subsequent requests are unaffected, and the device remains
+     * operational. ext2 contains the frame number of the request, and ext3
+     * contains the stream id.
+     */
+    CAMERA2_MSG_ERROR_STREAM,
+    /**
+     * Number of error types
+     */
+    CAMERA2_MSG_NUM_ERRORS
+};
+
+/**
+ * Possible trigger ids for trigger_action()
+ */
+enum {
+    /**
+     * Trigger an autofocus cycle. The effect of the trigger depends on the
+     * autofocus mode in effect when the trigger is received, which is the mode
+     * listed in the latest capture request to be dequeued. If the mode is off,
+     * the trigger has no effect. If autofocus is already scanning, the trigger
+     * has no effect. In AUTO, MACRO, or CONTINUOUS_* modes, the trigger
+     * otherwise begins an appropriate scan of the scene for focus. The state of
+     * the autofocus cycle can be tracked in android.control.afMode and the
+     * corresponding notification. Ext1 is an id that must be returned in
+     * subsequent auto-focus state change notifications.
+     */
+    CAMERA2_TRIGGER_AUTOFOCUS = 0x0001,
+    /**
+     * Trigger a pre-capture metering cycle, which may include firing the flash
+     * to determine proper capture parameters. Typically, this trigger would be
+     * fired for a half-depress of a camera shutter key, or before a snapshot
+     * capture in general. The state of the metering cycle can be tracked in
+     * android.control.aeMode and the corresponding notification.  If the
+     * auto-exposure mode is OFF, the trigger does nothing. Ext1 is an id that
+     * must be returned in subsequent auto-exposure state change notifications.
+     */
+     CAMERA2_TRIGGER_PRECAPTURE_METERING
+};
+
+/**
+ * Possible template types for construct_default_request()
+ */
+enum {
+    /**
+     * Standard camera preview operation with 3A on auto.
+     */
+    CAMERA2_TEMPLATE_PREVIEW = 1,
+    /**
+     * Standard camera high-quality still capture with 3A and flash on auto.
+     */
+    CAMERA2_TEMPLATE_STILL_CAPTURE,
+    /**
+     * Standard video recording plus preview with 3A on auto, torch off.
+     */
+    CAMERA2_TEMPLATE_VIDEO_RECORD,
+    /**
+     * High-quality still capture while recording video. Application will
+     * include preview, video record, and full-resolution YUV or JPEG streams in
+     * request. Must not cause stuttering on video stream. 3A on auto.
+     */
+    CAMERA2_TEMPLATE_VIDEO_SNAPSHOT,
+    /**
+     * Zero-shutter-lag mode. Application will request preview and
+     * full-resolution YUV data for each frame, and reprocess it to JPEG when a
+     * still image is requested by user. Settings should provide highest-quality
+     * full-resolution images without compromising preview frame rate. 3A on
+     * auto.
+     */
+    CAMERA2_TEMPLATE_ZERO_SHUTTER_LAG,
+
+    /* Total number of templates */
+    CAMERA2_TEMPLATE_COUNT
+};
+
+
+/**********************************************************************
+ *
+ * Camera device operations
+ *
+ */
+typedef struct camera2_device_ops {
+
+    /**********************************************************************
+     * Request and frame queue setup and management methods
+     */
+
+    /**
+     * Pass in input request queue interface methods.
+     */
+    int (*set_request_queue_src_ops)(const struct camera2_device *,
+            const camera2_request_queue_src_ops_t *request_src_ops);
+
+    /**
+     * Notify device that the request queue is no longer empty. Must only be
+     * called when the first buffer is added a new queue, or after the source
+     * has returned NULL in response to a dequeue call.
+     */
+    int (*notify_request_queue_not_empty)(const struct camera2_device *);
+
+    /**
+     * Pass in output frame queue interface methods
+     */
+    int (*set_frame_queue_dst_ops)(const struct camera2_device *,
+            const camera2_frame_queue_dst_ops_t *frame_dst_ops);
+
+    /**
+     * Number of camera requests being processed by the device at the moment
+     * (captures/reprocesses that have had their request dequeued, but have not
+     * yet been enqueued onto output pipeline(s) ). No streams may be released
+     * by the framework until the in-progress count is 0.
+     */
+    int (*get_in_progress_count)(const struct camera2_device *);
+
+    /**
+     * Flush all in-progress captures. This includes all dequeued requests
+     * (regular or reprocessing) that have not yet placed any outputs into a
+     * stream or the frame queue. Partially completed captures must be completed
+     * normally. No new requests may be dequeued from the request queue until
+     * the flush completes.
+     */
+    int (*flush_captures_in_progress)(const struct camera2_device *);
+
+    /**
+     * Create a filled-in default request for standard camera use cases.
+     *
+     * The device must return a complete request that is configured to meet the
+     * requested use case, which must be one of the CAMERA2_TEMPLATE_*
+     * enums. All request control fields must be included, except for
+     * android.request.outputStreams.
+     *
+     * The metadata buffer returned must be allocated with
+     * allocate_camera_metadata. The framework takes ownership of the buffer.
+     */
+    int (*construct_default_request)(const struct camera2_device *,
+            int request_template,
+            camera_metadata_t **request);
+
+    /**********************************************************************
+     * Stream management
+     */
+
+    /**
+     * allocate_stream:
+     *
+     * Allocate a new output stream for use, defined by the output buffer width,
+     * height, target, and possibly the pixel format.  Returns the new stream's
+     * ID, gralloc usage flags, minimum queue buffer count, and possibly the
+     * pixel format, on success. Error conditions:
+     *
+     *  - Requesting a width/height/format combination not listed as
+     *    supported by the sensor's static characteristics
+     *
+     *  - Asking for too many streams of a given format type (2 bayer raw
+     *    streams, for example).
+     *
+     * Input parameters:
+     *
+     * - width, height, format: Specification for the buffers to be sent through
+     *   this stream. Format is a value from the HAL_PIXEL_FORMAT_* list, or
+     *   CAMERA2_HAL_PIXEL_FORMAT_OPAQUE. In the latter case, the camera device
+     *   must select an appropriate (possible platform-specific) HAL pixel
+     *   format to return in format_actual. In the former case, format_actual
+     *   must be set to match format.
+     *
+     * - stream_ops: A structure of function pointers for obtaining and queuing
+     *   up buffers for this stream. The underlying stream will be configured
+     *   based on the usage and max_buffers outputs. The methods in this
+     *   structure may not be called until after allocate_stream returns.
+     *
+     * Output parameters:
+     *
+     * - stream_id: An unsigned integer identifying this stream. This value is
+     *   used in incoming requests to identify the stream, and in releasing the
+     *   stream.
+     *
+     * - format_actual: If the input format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE,
+     *   then device must select the appropriate (possible platform-specific)
+     *   pixel format and return it in *format_actual. It will be treated as an
+     *   opaque value by the framework, and simply passed to the gralloc module
+     *   when new buffers need to be allocated. If the input format is one of
+     *   the values from HAL_PIXEL_FORMAT_* list, then *format_actual must be
+     *   set equal to format. In the latter case, format_actual may also be
+     *   NULL, in which case it can be ignored as an output.
+     *
+     * - usage: The gralloc usage mask needed by the HAL device for producing
+     *   the requested type of data. This is used in allocating new gralloc
+     *   buffers for the stream buffer queue.
+     *
+     * - max_buffers: The maximum number of buffers the HAL device may need to
+     *   have dequeued at the same time. The device may not dequeue more buffers
+     *   than this value at the same time.
+     *
+     */
+    int (*allocate_stream)(
+            const struct camera2_device *,
+            // inputs
+            uint32_t width,
+            uint32_t height,
+            int      format,
+            const camera2_stream_ops_t *stream_ops,
+            // outputs
+            uint32_t *stream_id,
+            uint32_t *format_actual,
+            uint32_t *usage,
+            uint32_t *max_buffers);
+
+    /**
+     * Register buffers for a given stream. This is called after a successful
+     * allocate_stream call, and before the first request referencing the stream
+     * is enqueued. This method is intended to allow the HAL device to map or
+     * otherwise prepare the buffers for later use. num_buffers is guaranteed to
+     * be at least max_buffers (from allocate_stream), but may be larger. The
+     * buffers will already be locked for use. At the end of the call, all the
+     * buffers must be ready to be returned to the queue.
+     */
+    int (*register_stream_buffers)(
+            const struct camera2_device *,
+            uint32_t stream_id,
+            int num_buffers,
+            buffer_handle_t *buffers);
+
+    /**
+     * Release a stream. Returns an error if called when get_in_progress_count
+     * is non-zero, or if the stream id is invalid.
+     */
+    int (*release_stream)(
+            const struct camera2_device *,
+            uint32_t stream_id);
+
+    /**
+     * allocate_reprocess_stream:
+     *
+     * Allocate a new input stream for use, defined by the output buffer width,
+     * height, and the pixel format.  Returns the new stream's ID, gralloc usage
+     * flags, and required simultaneously acquirable buffer count, on
+     * success. Error conditions:
+     *
+     *  - Requesting a width/height/format combination not listed as
+     *    supported by the sensor's static characteristics
+     *
+     *  - Asking for too many reprocessing streams to be configured at once.
+     *
+     * Input parameters:
+     *
+     * - width, height, format: Specification for the buffers to be sent through
+     *   this stream. Format must be a value from the HAL_PIXEL_FORMAT_* list.
+     *
+     * - reprocess_stream_ops: A structure of function pointers for acquiring
+     *   and releasing buffers for this stream. The underlying stream will be
+     *   configured based on the usage and max_buffers outputs.
+     *
+     * Output parameters:
+     *
+     * - stream_id: An unsigned integer identifying this stream. This value is
+     *   used in incoming requests to identify the stream, and in releasing the
+     *   stream. These ids are numbered separately from the input stream ids.
+     *
+     * - consumer_usage: The gralloc usage mask needed by the HAL device for
+     *   consuming the requested type of data. This is used in allocating new
+     *   gralloc buffers for the stream buffer queue.
+     *
+     * - max_buffers: The maximum number of buffers the HAL device may need to
+     *   have acquired at the same time. The device may not have more buffers
+     *   acquired at the same time than this value.
+     *
+     */
+    int (*allocate_reprocess_stream)(const struct camera2_device *,
+            uint32_t width,
+            uint32_t height,
+            uint32_t format,
+            const camera2_stream_in_ops_t *reprocess_stream_ops,
+            // outputs
+            uint32_t *stream_id,
+            uint32_t *consumer_usage,
+            uint32_t *max_buffers);
+
+    /**
+     * Release a reprocessing stream. Returns an error if called when
+     * get_in_progress_count is non-zero, or if the stream id is not
+     * valid.
+     */
+    int (*release_reprocess_stream)(
+            const struct camera2_device *,
+            uint32_t stream_id);
+
+    /**********************************************************************
+     * Miscellaneous methods
+     */
+
+    /**
+     * Trigger asynchronous activity. This is used for triggering special
+     * behaviors of the camera 3A routines when they are in use. See the
+     * documentation for CAMERA2_TRIGGER_* above for details of the trigger ids
+     * and their arguments.
+     */
+    int (*trigger_action)(const struct camera2_device *,
+            uint32_t trigger_id,
+            int ext1,
+            int ext2);
+
+    /**
+     * Notification callback setup
+     */
+    int (*set_notify_callback)(const struct camera2_device *,
+            camera2_notify_callback notify_cb,
+            void *user);
+
+    /**
+     * Get methods to query for vendor extension metadata tag infomation. May
+     * set ops to NULL if no vendor extension tags are defined.
+     */
+    int (*get_metadata_vendor_tag_ops)(const struct camera2_device*,
+            vendor_tag_query_ops_t **ops);
+
+    /**
+     * Dump state of the camera hardware
+     */
+    int (*dump)(const struct camera2_device *, int fd);
+
+} camera2_device_ops_t;
+
+/**********************************************************************
+ *
+ * Camera device definition
+ *
+ */
+typedef struct camera2_device {
+    /**
+     * common.version must equal CAMERA_DEVICE_API_VERSION_2_0 to identify
+     * this device as implementing version 2.0 of the camera device HAL.
+     */
+    hw_device_t common;
+    camera2_device_ops_t *ops;
+    void *priv;
+} camera2_device_t;
+
+__END_DECLS
+
+#endif /* #ifdef ANDROID_INCLUDE_CAMERA2_H */
diff --git a/include/hardware/camera_common.h b/include/hardware/camera_common.h
new file mode 100644
index 0000000..5459b6c
--- /dev/null
+++ b/include/hardware/camera_common.h
@@ -0,0 +1,164 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// FIXME: add well-defined names for cameras
+
+#ifndef ANDROID_INCLUDE_CAMERA_COMMON_H
+#define ANDROID_INCLUDE_CAMERA_COMMON_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+#include <cutils/native_handle.h>
+#include <system/camera.h>
+#include <hardware/hardware.h>
+#include <hardware/gralloc.h>
+
+__BEGIN_DECLS
+
+/**
+ * The id of this module
+ */
+#define CAMERA_HARDWARE_MODULE_ID "camera"
+
+/**
+ * Module versioning information for the Camera hardware module, based on
+ * camera_module_t.common.module_api_version. The two most significant hex
+ * digits represent the major version, and the two least significant represent
+ * the minor version.
+ *
+ *******************************************************************************
+ * Versions: 0.X - 1.X [CAMERA_MODULE_API_VERSION_1_0]
+ *
+ *   Camera modules that report these version numbers implement the initial
+ *   camera module HAL interface. All camera devices openable through this
+ *   module support only version 1 of the camera device HAL. The device_version
+ *   and static_camera_characteristics fields of camera_info are not valid. Only
+ *   the android.hardware.Camera API can be supported by this module and its
+ *   devices.
+ *
+ *******************************************************************************
+ * Version: 2.0 [CAMERA_MODULE_API_VERSION_2_0]
+ *
+ *   Camera modules that report this version number implement the second version
+ *   of the camera module HAL interface. Camera devices openable through this
+ *   module may support either version 1.0 or version 2.0 of the camera device
+ *   HAL interface. The device_version field of camera_info is always valid; the
+ *   static_camera_characteristics field of camera_info is valid if the
+ *   device_version field is 2.0 or higher.
+ */
+
+/**
+ * Predefined macros for currently-defined version numbers
+ */
+
+/**
+ * All module versions <= HARDWARE_MODULE_API_VERSION(1, 0xFF) must be treated
+ * as CAMERA_MODULE_API_VERSION_1_0
+ */
+#define CAMERA_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(1, 0)
+#define CAMERA_MODULE_API_VERSION_2_0 HARDWARE_MODULE_API_VERSION(2, 0)
+
+#define CAMERA_MODULE_API_VERSION_CURRENT CAMERA_MODULE_API_VERSION_2_0
+
+/**
+ * All device versions <= HARDWARE_DEVICE_API_VERSION(1, 0xFF) must be treated
+ * as CAMERA_DEVICE_API_VERSION_1_0
+ */
+#define CAMERA_DEVICE_API_VERSION_1_0 HARDWARE_DEVICE_API_VERSION(1, 0)
+#define CAMERA_DEVICE_API_VERSION_2_0 HARDWARE_DEVICE_API_VERSION(2, 0)
+
+// Device version 2.0 is experimental
+#define CAMERA_DEVICE_API_VERSION_CURRENT CAMERA_DEVICE_API_VERSION_1_0
+
+/**
+ * Defined in /system/media/camera/include/system/camera_metadata.h
+ */
+typedef struct camera_metadata camera_metadata_t;
+
+struct camera_info {
+    /**
+     * The direction that the camera faces to. It should be CAMERA_FACING_BACK
+     * or CAMERA_FACING_FRONT.
+     *
+     * Version information:
+     *   Valid in all camera_module versions
+     */
+    int facing;
+
+    /**
+     * The orientation of the camera image. The value is the angle that the
+     * camera image needs to be rotated clockwise so it shows correctly on the
+     * display in its natural orientation. It should be 0, 90, 180, or 270.
+     *
+     * For example, suppose a device has a naturally tall screen. The
+     * back-facing camera sensor is mounted in landscape. You are looking at
+     * the screen. If the top side of the camera sensor is aligned with the
+     * right edge of the screen in natural orientation, the value should be
+     * 90. If the top side of a front-facing camera sensor is aligned with the
+     * right of the screen, the value should be 270.
+     *
+     * Version information:
+     *   Valid in all camera_module versions
+     */
+    int orientation;
+
+    /**
+     * The value of camera_device_t.common.version.
+     *
+     * Version information (based on camera_module_t.common.module_api_version):
+     *
+     *  CAMERA_MODULE_API_VERSION_1_0:
+     *
+     *    Not valid. Can be assumed to be CAMERA_DEVICE_API_VERSION_1_0. Do
+     *    not read this field.
+     *
+     *  CAMERA_MODULE_API_VERSION_2_0:
+     *
+     *    Always valid
+     *
+     */
+    uint32_t device_version;
+
+    /**
+     * The camera's fixed characteristics, which include all camera metadata in
+     * the android.*.info.* sections.
+     *
+     * Version information (based on camera_module_t.common.module_api_version):
+     *
+     *  CAMERA_MODULE_API_VERSION_1_0:
+     *
+     *    Not valid. Extra characteristics are not available. Do not read this
+     *    field.
+     *
+     *  CAMERA_MODULE_API_VERSION_2_0:
+     *
+     *    Valid if device_version >= CAMERA_DEVICE_API_VERSION_2_0. Do not read
+     *    otherwise.
+     *
+     */
+    camera_metadata_t *static_camera_characteristics;
+};
+
+typedef struct camera_module {
+    hw_module_t common;
+    int (*get_number_of_cameras)(void);
+    int (*get_camera_info)(int camera_id, struct camera_info *info);
+} camera_module_t;
+
+__END_DECLS
+
+#endif /* ANDROID_INCLUDE_CAMERA_COMMON_H */
diff --git a/include/hardware/gralloc.h b/include/hardware/gralloc.h
index f8beb5e..86ed95c 100644
--- a/include/hardware/gralloc.h
+++ b/include/hardware/gralloc.h
@@ -76,8 +76,12 @@
     GRALLOC_USAGE_HW_FB                 = 0x00001000,
     /* buffer will be used with the HW video encoder */
     GRALLOC_USAGE_HW_VIDEO_ENCODER      = 0x00010000,
+    /* buffer will be written by the HW camera pipeline */
+    GRALLOC_USAGE_HW_CAMERA_WRITE       = 0x00020000,
+    /* buffer will be read by the HW camera pipeline */
+    GRALLOC_USAGE_HW_CAMERA_READ        = 0x00040000,
     /* mask for the software usage bit-mask */
-    GRALLOC_USAGE_HW_MASK               = 0x00011F00,
+    GRALLOC_USAGE_HW_MASK               = 0x00071F00,
 
     /* buffer should be displayed full-screen on an external display when
      * possible
@@ -258,4 +262,4 @@
 
 __END_DECLS
 
-#endif  // ANDROID_ALLOC_INTERFACE_H
+#endif  // ANDROID_GRALLOC_INTERFACE_H
diff --git a/include/hardware/hardware.h b/include/hardware/hardware.h
index 7774b2b..78c4572 100644
--- a/include/hardware/hardware.h
+++ b/include/hardware/hardware.h
@@ -34,6 +34,38 @@
 #define HARDWARE_MODULE_TAG MAKE_TAG_CONSTANT('H', 'W', 'M', 'T')
 #define HARDWARE_DEVICE_TAG MAKE_TAG_CONSTANT('H', 'W', 'D', 'T')
 
+#define HARDWARE_MAKE_API_VERSION(maj,min) \
+            ((((maj) & 0xff) << 8) | ((min) & 0xff))
+
+/*
+ * The current HAL API version.
+ *
+ * All module implementations must set the hw_module_t.hal_api_version field
+ * to this value when declaring the module with HAL_MODULE_INFO_SYM.
+ *
+ * Note that previous implementations have always set this field to 0.
+ * Therefore, libhardware HAL API will always consider versions 0.0 and 1.0
+ * to be 100% binary compatible.
+ *
+ */
+#define HARDWARE_HAL_API_VERSION HARDWARE_MAKE_API_VERSION(1, 0)
+
+/*
+ * Helper macros for module implementors.
+ *
+ * The derived modules should provide convenience macros for supported
+ * versions so that implementations can explicitly specify module/device
+ * versions at definition time.
+ *
+ * Use this macro to set the hw_module_t.module_api_version field.
+ */
+#define HARDWARE_MODULE_API_VERSION(maj,min) HARDWARE_MAKE_API_VERSION(maj,min)
+
+/*
+ * Use this macro to set the hw_device_t.version field
+ */
+#define HARDWARE_DEVICE_API_VERSION(maj,min) HARDWARE_MAKE_API_VERSION(maj,min)
+
 struct hw_module_t;
 struct hw_module_methods_t;
 struct hw_device_t;
@@ -47,11 +79,47 @@
     /** tag must be initialized to HARDWARE_MODULE_TAG */
     uint32_t tag;
 
-    /** major version number for the module */
-    uint16_t version_major;
+    /**
+     * The API version of the implemented module. The module owner is
+     * responsible for updating the version when a module interface has
+     * changed.
+     *
+     * The derived modules such as gralloc and audio own and manage this field.
+     * The module user must interpret the version field to decide whether or
+     * not to inter-operate with the supplied module implementation.
+     * For example, SurfaceFlinger is responsible for making sure that
+     * it knows how to manage different versions of the gralloc-module API,
+     * and AudioFlinger must know how to do the same for audio-module API.
+     *
+     * The module API version should include a major and a minor component.
+     * For example, version 1.0 could be represented as 0x0100. This format
+     * implies that versions 0x0100-0x01ff are all API-compatible.
+     *
+     * In the future, libhardware will expose a hw_get_module_version()
+     * (or equivalent) function that will take minimum/maximum supported
+     * versions as arguments and would be able to reject modules with
+     * versions outside of the supplied range.
+     */
+    uint16_t module_api_version;
+#define version_major module_api_version
+    /**
+     * version_major/version_minor defines are supplied here for temporary
+     * source code compatibility. They will be removed in the next version.
+     * ALL clients must convert to the new version format.
+     */
 
-    /** minor version number of the module */
-    uint16_t version_minor;
+    /**
+     * The API version of the HAL module interface. This is meant to
+     * version the hw_module_t, hw_module_methods_t, and hw_device_t
+     * structures and definitions.
+     *
+     * The HAL interface owns this field. Module users/implementations
+     * must NOT rely on this value for version information.
+     *
+     * Presently, 0 is the only valid value.
+     */
+    uint16_t hal_api_version;
+#define version_minor hal_api_version
 
     /** Identifier of module */
     const char *id;
@@ -88,7 +156,22 @@
     /** tag must be initialized to HARDWARE_DEVICE_TAG */
     uint32_t tag;
 
-    /** version number for hw_device_t */
+    /**
+     * Version of the module-specific device API. This value is used by
+     * the derived-module user to manage different device implementations.
+     *
+     * The module user is responsible for checking the module_api_version
+     * and device version fields to ensure that the user is capable of
+     * communicating with the specific module implementation.
+     *
+     * One module can support multiple devices with different versions. This
+     * can be useful when a device interface changes in an incompatible way
+     * but it is still necessary to support older implementations at the same
+     * time. One such example is the Camera 2.0 API.
+     *
+     * This field is interpreted by the module user and is ignored by the
+     * HAL interface itself.
+     */
     uint32_t version;
 
     /** reference to the module this device belongs to */
diff --git a/include/hardware/hwcomposer.h b/include/hardware/hwcomposer.h
index b4a7a7a..a036da3 100644
--- a/include/hardware/hwcomposer.h
+++ b/include/hardware/hwcomposer.h
@@ -24,11 +24,30 @@
 #include <hardware/hardware.h>
 #include <cutils/native_handle.h>
 
+#include <hardware/hwcomposer_defs.h>
+
 __BEGIN_DECLS
 
 /*****************************************************************************/
 
-#define HWC_API_VERSION 1
+/* for compatibility */
+#define HWC_MODULE_API_VERSION      HWC_MODULE_API_VERSION_0_1
+#define HWC_DEVICE_API_VERSION      HWC_DEVICE_API_VERSION_0_1
+#define HWC_API_VERSION             HWC_DEVICE_API_VERSION
+
+/* Users of this header can define HWC_REMOVE_DEPRECATED_VERSIONS to test that
+ * they still work with just the current version declared, before the
+ * deprecated versions are actually removed.
+ *
+ * To find code that still depends on the old versions, set the #define to 1
+ * here. Code that explicitly sets it to zero (rather than simply not defining
+ * it) will still see the old versions.
+ */
+#if !defined(HWC_REMOVE_DEPRECATED_VERSIONS)
+#define HWC_REMOVE_DEPRECATED_VERSIONS 0
+#endif
+
+/*****************************************************************************/
 
 /**
  * The id of this module
@@ -40,88 +59,42 @@
  */
 #define HWC_HARDWARE_COMPOSER   "composer"
 
-
-enum {
-    /* hwc_composer_device_t::set failed in EGL */
-    HWC_EGL_ERROR = -1
-};
-
-/*
- * hwc_layer_t::hints values
- * Hints are set by the HAL and read by SurfaceFlinger
- */
-enum {
-    /*
-     * HWC can set the HWC_HINT_TRIPLE_BUFFER hint to indicate to SurfaceFlinger
-     * that it should triple buffer this layer. Typically HWC does this when
-     * the layer will be unavailable for use for an extended period of time,
-     * e.g. if the display will be fetching data directly from the layer and
-     * the layer can not be modified until after the next set().
-     */
-    HWC_HINT_TRIPLE_BUFFER  = 0x00000001,
+struct hwc_composer_device_1;
+typedef struct hwc_methods_1 {
 
     /*
-     * HWC sets HWC_HINT_CLEAR_FB to tell SurfaceFlinger that it should clear the
-     * framebuffer with transparent pixels where this layer would be.
-     * SurfaceFlinger will only honor this flag when the layer has no blending
+     * eventControl(..., event, enabled)
+     * Enables or disables h/w composer events.
      *
+     * eventControl can be called from any thread and takes effect
+     * immediately.
+     *
+     *  Supported events are:
+     *      HWC_EVENT_VSYNC
+     *
+     * returns -EINVAL if the "event" parameter is not one of the value above
+     * or if the "enabled" parameter is not 0 or 1.
      */
-    HWC_HINT_CLEAR_FB       = 0x00000002
-};
 
-/*
- * hwc_layer_t::flags values
- * Flags are set by SurfaceFlinger and read by the HAL
- */
-enum {
+    int (*eventControl)(
+            struct hwc_composer_device_1* dev, int event, int enabled);
+
     /*
-     * HWC_SKIP_LAYER is set by SurfaceFlnger to indicate that the HAL
-     * shall not consider this layer for composition as it will be handled
-     * by SurfaceFlinger (just as if compositionType was set to HWC_OVERLAY).
+     * This field is OPTIONAL and can be NULL.
+     *
+     * blank(..., blank)
+     * Blanks or unblanks the screen.
+     *
+     * Turns the screen off when blank is nonzero, on when blank is zero.
+     * Blanking may also be triggered by a call to set..., 0, 0, 0).  Multiple
+     * sequential calls with the same blank value must be supported.
+     *
+     * returns 0 on success, negative on error.
      */
-    HWC_SKIP_LAYER = 0x00000001,
-};
 
-/*
- * hwc_layer_t::compositionType values
- */
-enum {
-    /* this layer is to be drawn into the framebuffer by SurfaceFlinger */
-    HWC_FRAMEBUFFER = 0,
+    int (*blank)(struct hwc_composer_device_1* dev, int blank);
 
-    /* this layer will be handled in the HWC */
-    HWC_OVERLAY = 1,
-};
-
-/*
- * hwc_layer_t::blending values
- */
-enum {
-    /* no blending */
-    HWC_BLENDING_NONE     = 0x0100,
-
-    /* ONE / ONE_MINUS_SRC_ALPHA */
-    HWC_BLENDING_PREMULT  = 0x0105,
-
-    /* SRC_ALPHA / ONE_MINUS_SRC_ALPHA */
-    HWC_BLENDING_COVERAGE = 0x0405
-};
-
-/*
- * hwc_layer_t::transform values
- */
-enum {
-    /* flip source image horizontally */
-    HWC_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
-    /* flip source image vertically */
-    HWC_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
-    /* rotate source image 90 degrees clock-wise */
-    HWC_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
-    /* rotate source image 180 degrees */
-    HWC_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
-    /* rotate source image 270 degrees clock-wise */
-    HWC_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
-};
+} hwc_methods_1_t;
 
 typedef struct hwc_rect {
     int left;
@@ -135,12 +108,29 @@
     hwc_rect_t const* rects;
 } hwc_region_t;
 
-typedef struct hwc_layer {
+typedef struct hwc_color {
+    uint8_t r;
+    uint8_t g;
+    uint8_t b;
+    uint8_t a;
+} hwc_color_t;
+
+typedef struct hwc_layer_1 {
     /*
-     * initially set to HWC_FRAMEBUFFER, indicates the layer will
-     * be drawn into the framebuffer using OpenGL ES.
-     * The HWC can toggle this value to HWC_OVERLAY, to indicate
-     * it will handle the layer.
+     * initially set to HWC_FRAMEBUFFER or HWC_BACKGROUND.
+     * HWC_FRAMEBUFFER
+     *   indicates the layer will be drawn into the framebuffer
+     *   using OpenGL ES.
+     *   The HWC can toggle this value to HWC_OVERLAY, to indicate
+     *   it will handle the layer.
+     *
+     * HWC_BACKGROUND
+     *   indicates this is a special "background"  layer. The only valid
+     *   field is backgroundColor. HWC_BACKGROUND can only be used with
+     *   HWC_API_VERSION >= 0.2
+     *   The HWC can toggle this value to HWC_FRAMEBUFFER, to indicate
+     *   it CANNOT handle the background color
+     *
      */
     int32_t compositionType;
 
@@ -150,33 +140,80 @@
     /* see hwc_layer_t::flags above */
     uint32_t flags;
 
-    /* handle of buffer to compose. this handle is guaranteed to have been
-     * allocated with gralloc */
-    buffer_handle_t handle;
+    union {
+        /* color of the background.  hwc_color_t.a is ignored */
+        hwc_color_t backgroundColor;
 
-    /* transformation to apply to the buffer during composition */
-    uint32_t transform;
+        struct {
+            /* handle of buffer to compose. This handle is guaranteed to have been
+             * allocated from gralloc using the GRALLOC_USAGE_HW_COMPOSER usage flag. If
+             * the layer's handle is unchanged across two consecutive prepare calls and
+             * the HWC_GEOMETRY_CHANGED flag is not set for the second call then the
+             * HWComposer implementation may assume that the contents of the buffer have
+             * not changed. */
+            buffer_handle_t handle;
 
-    /* blending to apply during composition */
-    int32_t blending;
+            /* transformation to apply to the buffer during composition */
+            uint32_t transform;
 
-    /* area of the source to consider, the origin is the top-left corner of
-     * the buffer */
-    hwc_rect_t sourceCrop;
+            /* blending to apply during composition */
+            int32_t blending;
 
-    /* where to composite the sourceCrop onto the display. The sourceCrop
-     * is scaled using linear filtering to the displayFrame. The origin is the
-     * top-left corner of the screen.
+            /* area of the source to consider, the origin is the top-left corner of
+             * the buffer */
+            hwc_rect_t sourceCrop;
+
+            /* where to composite the sourceCrop onto the display. The sourceCrop
+             * is scaled using linear filtering to the displayFrame. The origin is the
+             * top-left corner of the screen.
+             */
+            hwc_rect_t displayFrame;
+
+            /* visible region in screen space. The origin is the
+             * top-left corner of the screen.
+             * The visible region INCLUDES areas overlapped by a translucent layer.
+             */
+            hwc_region_t visibleRegionScreen;
+
+            /* Sync fence object that will be signaled when the buffer's
+             * contents are available. May be -1 if the contents are already
+             * available. This field is only valid during set(), and should be
+             * ignored during prepare(). The set() call must not wait for the
+             * fence to be signaled before returning, but the HWC must wait for
+             * all buffers to be signaled before reading from them.
+             *
+             * The HWC takes ownership of the acquireFenceFd and is responsible
+             * for closing it when no longer needed.
+             */
+            int acquireFenceFd;
+
+            /* During set() the HWC must set this field to a file descriptor for
+             * a sync fence object that will signal after the HWC has finished
+             * reading from the buffer. The field is ignored by prepare(). Each
+             * layer should have a unique file descriptor, even if more than one
+             * refer to the same underlying fence object; this allows each to be
+             * closed independently.
+             *
+             * If buffer reads can complete at significantly different times,
+             * then using independent fences is preferred. For example, if the
+             * HWC handles some layers with a blit engine and others with
+             * overlays, then the blit layers can be reused immediately after
+             * the blit completes, but the overlay layers can't be reused until
+             * a subsequent frame has been displayed.
+             *
+             * The HWC client taks ownership of the releaseFenceFd and is
+             * responsible for closing it when no longer needed.
+             */
+            int releaseFenceFd;
+        };
+    };
+
+    /* Allow for expansion w/o breaking binary compatibility.
+     * Pad layer to 96 bytes, assuming 32-bit pointers.
      */
-    hwc_rect_t displayFrame;
+    int32_t reserved[24 - 18];
 
-    /* visible region in screen space. The origin is the
-     * top-left corner of the screen.
-     * The visible region INCLUDES areas overlapped by a translucent layer.
-     */
-    hwc_region_t visibleRegionScreen;
-} hwc_layer_t;
-
+} hwc_layer_1_t;
 
 /*
  * hwc_layer_list_t::flags values
@@ -184,7 +221,8 @@
 enum {
     /*
      * HWC_GEOMETRY_CHANGED is set by SurfaceFlinger to indicate that the list
-     * passed to (*prepare)() has changed by more than just the buffer handles.
+     * passed to (*prepare)() has changed by more than just the buffer handles
+     * and acquire fences.
      */
     HWC_GEOMETRY_CHANGED = 0x00000001,
 };
@@ -193,11 +231,11 @@
  * List of layers.
  * The handle members of hwLayers elements must be unique.
  */
-typedef struct hwc_layer_list {
+typedef struct hwc_layer_list_1 {
     uint32_t flags;
     size_t numHwLayers;
-    hwc_layer_t hwLayers[0];
-} hwc_layer_list_t;
+    hwc_layer_1_t hwLayers[0];
+} hwc_layer_list_1_t;
 
 /* This represents a display, typically an EGLDisplay object */
 typedef void* hwc_display_t;
@@ -221,6 +259,28 @@
      * hooks, unless noted otherwise.
      */
     void (*invalidate)(struct hwc_procs* procs);
+
+    /*
+     * (*vsync)() is called by the h/w composer HAL when a vsync event is
+     * received and HWC_EVENT_VSYNC is enabled (see: hwc_event_control).
+     *
+     * the "zero" parameter must always be 0.
+     * the "timestamp" parameter is the system monotonic clock timestamp in
+     *   nanosecond of when the vsync event happened.
+     *
+     * vsync() is GUARANTEED TO NOT CALL BACK into the h/w composer HAL.
+     *
+     * It is expected that vsync() is called from a thread of at least
+     * HAL_PRIORITY_URGENT_DISPLAY with as little latency as possible,
+     * typically less than 0.5 ms.
+     *
+     * It is a (silent) error to have HWC_EVENT_VSYNC enabled when calling
+     * hwc_composer_device.set(..., 0, 0, 0) (screen off). The implementation
+     * can either stop or continue to process VSYNC events, but must not
+     * crash or cause other problems.
+     *
+     */
+    void (*vsync)(struct hwc_procs* procs, int zero, int64_t timestamp);
 } hwc_procs_t;
 
 
@@ -230,8 +290,7 @@
     struct hw_module_t common;
 } hwc_module_t;
 
-
-typedef struct hwc_composer_device {
+typedef struct hwc_composer_device_1 {
     struct hw_device_t common;
 
     /*
@@ -257,8 +316,8 @@
      * returned, SurfaceFlinger will assume that none of the layer will be
      * handled by the HWC.
      */
-    int (*prepare)(struct hwc_composer_device *dev, hwc_layer_list_t* list);
-
+    int (*prepare)(struct hwc_composer_device_1 *dev,
+                    hwc_layer_list_1_t* list);
 
     /*
      * (*set)() is used in place of eglSwapBuffers(), and assumes the same
@@ -302,19 +361,20 @@
      *    Another code for non EGL errors.
      *
      */
-    int (*set)(struct hwc_composer_device *dev,
+    int (*set)(struct hwc_composer_device_1 *dev,
                 hwc_display_t dpy,
                 hwc_surface_t sur,
-                hwc_layer_list_t* list);
+                hwc_layer_list_1_t* list);
+
     /*
-     * This hook is OPTIONAL.
+     * This field is OPTIONAL and can be NULL.
      *
      * If non NULL it will be called by SurfaceFlinger on dumpsys
      */
-    void (*dump)(struct hwc_composer_device* dev, char *buff, int buff_len);
+    void (*dump)(struct hwc_composer_device_1* dev, char *buff, int buff_len);
 
     /*
-     * This hook is OPTIONAL.
+     * This field is OPTIONAL and can be NULL.
      *
      * (*registerProcs)() registers a set of callbacks the h/w composer HAL
      * can later use. It is FORBIDDEN to call any of the callbacks from
@@ -326,29 +386,48 @@
      * Any of the callbacks can be NULL, in which case the corresponding
      * functionality is not supported.
      */
-    void (*registerProcs)(struct hwc_composer_device* dev,
+    void (*registerProcs)(struct hwc_composer_device_1* dev,
             hwc_procs_t const* procs);
 
-    void* reserved_proc[6];
+    /*
+     * This field is OPTIONAL and can be NULL.
+     *
+     * Used to retrieve information about the h/w composer
+     *
+     * Returns 0 on success or -errno on error.
+     */
+    int (*query)(struct hwc_composer_device_1* dev, int what, int* value);
 
-} hwc_composer_device_t;
+    /*
+     * Reserved for future use. Must be NULL.
+     */
+    void* reserved_proc[4];
 
+    /*
+     * This field is OPTIONAL and can be NULL.
+     */
+    hwc_methods_1_t const *methods;
+
+} hwc_composer_device_1_t;
 
 /** convenience API for opening and closing a device */
 
-static inline int hwc_open(const struct hw_module_t* module,
-        hwc_composer_device_t** device) {
+static inline int hwc_open_1(const struct hw_module_t* module,
+        hwc_composer_device_1_t** device) {
     return module->methods->open(module,
             HWC_HARDWARE_COMPOSER, (struct hw_device_t**)device);
 }
 
-static inline int hwc_close(hwc_composer_device_t* device) {
+static inline int hwc_close_1(hwc_composer_device_1_t* device) {
     return device->common.close(&device->common);
 }
 
-
 /*****************************************************************************/
 
+#if !HWC_REMOVE_DEPRECATED_VERSIONS
+#include <hardware/hwcomposer_v0.h>
+#endif
+
 __END_DECLS
 
 #endif /* ANDROID_INCLUDE_HARDWARE_HWCOMPOSER_H */
diff --git a/include/hardware/hwcomposer_defs.h b/include/hardware/hwcomposer_defs.h
new file mode 100644
index 0000000..02b8e50
--- /dev/null
+++ b/include/hardware/hwcomposer_defs.h
@@ -0,0 +1,148 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_INCLUDE_HARDWARE_HWCOMPOSER_DEFS_H
+#define ANDROID_INCLUDE_HARDWARE_HWCOMPOSER_DEFS_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+
+#include <hardware/gralloc.h>
+#include <hardware/hardware.h>
+#include <cutils/native_handle.h>
+
+__BEGIN_DECLS
+
+/*****************************************************************************/
+
+#define HWC_MODULE_API_VERSION_0_1  HARDWARE_MODULE_API_VERSION(0, 1)
+
+#define HWC_DEVICE_API_VERSION_0_1  HARDWARE_DEVICE_API_VERSION(0, 1)
+#define HWC_DEVICE_API_VERSION_0_2  HARDWARE_DEVICE_API_VERSION(0, 2)
+#define HWC_DEVICE_API_VERSION_0_3  HARDWARE_DEVICE_API_VERSION(0, 3)
+#define HWC_DEVICE_API_VERSION_1_0  HARDWARE_DEVICE_API_VERSION(1, 0)
+
+enum {
+    /* hwc_composer_device_t::set failed in EGL */
+    HWC_EGL_ERROR = -1
+};
+
+/*
+ * hwc_layer_t::hints values
+ * Hints are set by the HAL and read by SurfaceFlinger
+ */
+enum {
+    /*
+     * HWC can set the HWC_HINT_TRIPLE_BUFFER hint to indicate to SurfaceFlinger
+     * that it should triple buffer this layer. Typically HWC does this when
+     * the layer will be unavailable for use for an extended period of time,
+     * e.g. if the display will be fetching data directly from the layer and
+     * the layer can not be modified until after the next set().
+     */
+    HWC_HINT_TRIPLE_BUFFER  = 0x00000001,
+
+    /*
+     * HWC sets HWC_HINT_CLEAR_FB to tell SurfaceFlinger that it should clear the
+     * framebuffer with transparent pixels where this layer would be.
+     * SurfaceFlinger will only honor this flag when the layer has no blending
+     *
+     */
+    HWC_HINT_CLEAR_FB       = 0x00000002
+};
+
+/*
+ * hwc_layer_t::flags values
+ * Flags are set by SurfaceFlinger and read by the HAL
+ */
+enum {
+    /*
+     * HWC_SKIP_LAYER is set by SurfaceFlnger to indicate that the HAL
+     * shall not consider this layer for composition as it will be handled
+     * by SurfaceFlinger (just as if compositionType was set to HWC_OVERLAY).
+     */
+    HWC_SKIP_LAYER = 0x00000001,
+};
+
+/*
+ * hwc_layer_t::compositionType values
+ */
+enum {
+    /* this layer is to be drawn into the framebuffer by SurfaceFlinger */
+    HWC_FRAMEBUFFER = 0,
+
+    /* this layer will be handled in the HWC */
+    HWC_OVERLAY = 1,
+
+    /* this is the background layer. it's used to set the background color.
+     * there is only a single background layer */
+    HWC_BACKGROUND = 2,
+};
+
+/*
+ * hwc_layer_t::blending values
+ */
+enum {
+    /* no blending */
+    HWC_BLENDING_NONE     = 0x0100,
+
+    /* ONE / ONE_MINUS_SRC_ALPHA */
+    HWC_BLENDING_PREMULT  = 0x0105,
+
+    /* SRC_ALPHA / ONE_MINUS_SRC_ALPHA */
+    HWC_BLENDING_COVERAGE = 0x0405
+};
+
+/*
+ * hwc_layer_t::transform values
+ */
+enum {
+    /* flip source image horizontally */
+    HWC_TRANSFORM_FLIP_H = HAL_TRANSFORM_FLIP_H,
+    /* flip source image vertically */
+    HWC_TRANSFORM_FLIP_V = HAL_TRANSFORM_FLIP_V,
+    /* rotate source image 90 degrees clock-wise */
+    HWC_TRANSFORM_ROT_90 = HAL_TRANSFORM_ROT_90,
+    /* rotate source image 180 degrees */
+    HWC_TRANSFORM_ROT_180 = HAL_TRANSFORM_ROT_180,
+    /* rotate source image 270 degrees clock-wise */
+    HWC_TRANSFORM_ROT_270 = HAL_TRANSFORM_ROT_270,
+};
+
+/* attributes queriable with query() */
+enum {
+    /*
+     * availability: HWC_DEVICE_API_VERSION_0_2
+     * must return 1 if the background layer is supported, 0 otherwise
+     */
+    HWC_BACKGROUND_LAYER_SUPPORTED      = 0,
+
+    /*
+     * availability: HWC_DEVICE_API_VERSION_0_3
+     * returns the vsync period in nanosecond
+     */
+    HWC_VSYNC_PERIOD                    = 1,
+};
+
+/* Allowed events for hwc_methods::eventControl() */
+enum {
+    HWC_EVENT_VSYNC     = 0
+};
+
+/*****************************************************************************/
+
+__END_DECLS
+
+#endif /* ANDROID_INCLUDE_HARDWARE_HWCOMPOSER_DEFS_H */
diff --git a/include/hardware/hwcomposer_v0.h b/include/hardware/hwcomposer_v0.h
new file mode 100644
index 0000000..473819b
--- /dev/null
+++ b/include/hardware/hwcomposer_v0.h
@@ -0,0 +1,272 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* This header contains deprecated HWCv0 interface declarations. Don't include
+ * this header directly; it will be included by <hardware/hwcomposer.h> unless
+ * HWC_REMOVE_DEPRECATED_VERSIONS is defined to non-zero.
+ */
+#ifndef ANDROID_INCLUDE_HARDWARE_HWCOMPOSER_H
+#error "This header should only be included by hardware/hwcomposer.h"
+#endif
+
+#ifndef ANDROID_INCLUDE_HARDWARE_HWCOMPOSER_V0_H
+#define ANDROID_INCLUDE_HARDWARE_HWCOMPOSER_V0_H
+
+struct hwc_composer_device;
+
+/*
+ * availability: HWC_DEVICE_API_VERSION_0_3
+ *
+ * struct hwc_methods cannot be embedded in other structures as
+ * sizeof(struct hwc_methods) cannot be relied upon.
+ *
+ */
+typedef struct hwc_methods {
+
+    /*************************************************************************
+     * HWC_DEVICE_API_VERSION_0_3
+     *************************************************************************/
+
+    /*
+     * eventControl(..., event, enabled)
+     * Enables or disables h/w composer events.
+     *
+     * eventControl can be called from any thread and takes effect
+     * immediately.
+     *
+     *  Supported events are:
+     *      HWC_EVENT_VSYNC
+     *
+     * returns -EINVAL if the "event" parameter is not one of the value above
+     * or if the "enabled" parameter is not 0 or 1.
+     */
+
+    int (*eventControl)(
+            struct hwc_composer_device* dev, int event, int enabled);
+
+} hwc_methods_t;
+
+typedef struct hwc_layer {
+    /*
+     * initially set to HWC_FRAMEBUFFER or HWC_BACKGROUND.
+     * HWC_FRAMEBUFFER
+     *   indicates the layer will be drawn into the framebuffer
+     *   using OpenGL ES.
+     *   The HWC can toggle this value to HWC_OVERLAY, to indicate
+     *   it will handle the layer.
+     *
+     * HWC_BACKGROUND
+     *   indicates this is a special "background"  layer. The only valid
+     *   field is backgroundColor. HWC_BACKGROUND can only be used with
+     *   HWC_API_VERSION >= 0.2
+     *   The HWC can toggle this value to HWC_FRAMEBUFFER, to indicate
+     *   it CANNOT handle the background color
+     *
+     */
+    int32_t compositionType;
+
+    /* see hwc_layer_t::hints above */
+    uint32_t hints;
+
+    /* see hwc_layer_t::flags above */
+    uint32_t flags;
+
+    union {
+        /* color of the background.  hwc_color_t.a is ignored */
+        hwc_color_t backgroundColor;
+
+        struct {
+            /* handle of buffer to compose. This handle is guaranteed to have been
+             * allocated from gralloc using the GRALLOC_USAGE_HW_COMPOSER usage flag. If
+             * the layer's handle is unchanged across two consecutive prepare calls and
+             * the HWC_GEOMETRY_CHANGED flag is not set for the second call then the
+             * HWComposer implementation may assume that the contents of the buffer have
+             * not changed. */
+            buffer_handle_t handle;
+
+            /* transformation to apply to the buffer during composition */
+            uint32_t transform;
+
+            /* blending to apply during composition */
+            int32_t blending;
+
+            /* area of the source to consider, the origin is the top-left corner of
+             * the buffer */
+            hwc_rect_t sourceCrop;
+
+            /* where to composite the sourceCrop onto the display. The sourceCrop
+             * is scaled using linear filtering to the displayFrame. The origin is the
+             * top-left corner of the screen.
+             */
+            hwc_rect_t displayFrame;
+
+            /* visible region in screen space. The origin is the
+             * top-left corner of the screen.
+             * The visible region INCLUDES areas overlapped by a translucent layer.
+             */
+            hwc_region_t visibleRegionScreen;
+        };
+    };
+} hwc_layer_t;
+
+/*
+ * List of layers.
+ * The handle members of hwLayers elements must be unique.
+ */
+typedef struct hwc_layer_list {
+    uint32_t flags;
+    size_t numHwLayers;
+    hwc_layer_t hwLayers[0];
+} hwc_layer_list_t;
+
+/*****************************************************************************/
+
+typedef struct hwc_composer_device {
+    struct hw_device_t common;
+
+    /*
+     * (*prepare)() is called for each frame before composition and is used by
+     * SurfaceFlinger to determine what composition steps the HWC can handle.
+     *
+     * (*prepare)() can be called more than once, the last call prevails.
+     *
+     * The HWC responds by setting the compositionType field to either
+     * HWC_FRAMEBUFFER or HWC_OVERLAY. In the former case, the composition for
+     * this layer is handled by SurfaceFlinger with OpenGL ES, in the later
+     * case, the HWC will have to handle this layer's composition.
+     *
+     * (*prepare)() is called with HWC_GEOMETRY_CHANGED to indicate that the
+     * list's geometry has changed, that is, when more than just the buffer's
+     * handles have been updated. Typically this happens (but is not limited to)
+     * when a window is added, removed, resized or moved.
+     *
+     * a NULL list parameter or a numHwLayers of zero indicates that the
+     * entire composition will be handled by SurfaceFlinger with OpenGL ES.
+     *
+     * returns: 0 on success. An negative error code on error. If an error is
+     * returned, SurfaceFlinger will assume that none of the layer will be
+     * handled by the HWC.
+     */
+    int (*prepare)(struct hwc_composer_device *dev, hwc_layer_list_t* list);
+
+    /*
+     * (*set)() is used in place of eglSwapBuffers(), and assumes the same
+     * functionality, except it also commits the work list atomically with
+     * the actual eglSwapBuffers().
+     *
+     * The list parameter is guaranteed to be the same as the one returned
+     * from the last call to (*prepare)().
+     *
+     * When this call returns the caller assumes that:
+     *
+     * - the display will be updated in the near future with the content
+     *   of the work list, without artifacts during the transition from the
+     *   previous frame.
+     *
+     * - all objects are available for immediate access or destruction, in
+     *   particular, hwc_region_t::rects data and hwc_layer_t::layer's buffer.
+     *   Note that this means that immediately accessing (potentially from a
+     *   different process) a buffer used in this call will not result in
+     *   screen corruption, the driver must apply proper synchronization or
+     *   scheduling (eg: block the caller, such as gralloc_module_t::lock(),
+     *   OpenGL ES, Camera, Codecs, etc..., or schedule the caller's work
+     *   after the buffer is freed from the actual composition).
+     *
+     * a NULL list parameter or a numHwLayers of zero indicates that the
+     * entire composition has been handled by SurfaceFlinger with OpenGL ES.
+     * In this case, (*set)() behaves just like eglSwapBuffers().
+     *
+     * dpy, sur, and list are set to NULL to indicate that the screen is
+     * turning off. This happens WITHOUT prepare() being called first.
+     * This is a good time to free h/w resources and/or power
+     * the relevant h/w blocks down.
+     *
+     * IMPORTANT NOTE: there is an implicit layer containing opaque black
+     * pixels behind all the layers in the list.
+     * It is the responsibility of the hwcomposer module to make
+     * sure black pixels are output (or blended from).
+     *
+     * returns: 0 on success. An negative error code on error:
+     *    HWC_EGL_ERROR: eglGetError() will provide the proper error code
+     *    Another code for non EGL errors.
+     *
+     */
+    int (*set)(struct hwc_composer_device *dev,
+                hwc_display_t dpy,
+                hwc_surface_t sur,
+                hwc_layer_list_t* list);
+
+    /*
+     * This field is OPTIONAL and can be NULL.
+     *
+     * If non NULL it will be called by SurfaceFlinger on dumpsys
+     */
+    void (*dump)(struct hwc_composer_device* dev, char *buff, int buff_len);
+
+    /*
+     * This field is OPTIONAL and can be NULL.
+     *
+     * (*registerProcs)() registers a set of callbacks the h/w composer HAL
+     * can later use. It is FORBIDDEN to call any of the callbacks from
+     * within registerProcs(). registerProcs() must save the hwc_procs_t pointer
+     * which is needed when calling a registered callback.
+     * Each call to registerProcs replaces the previous set of callbacks.
+     * registerProcs is called with NULL to unregister all callbacks.
+     *
+     * Any of the callbacks can be NULL, in which case the corresponding
+     * functionality is not supported.
+     */
+    void (*registerProcs)(struct hwc_composer_device* dev,
+            hwc_procs_t const* procs);
+
+    /*
+     * This field is OPTIONAL and can be NULL.
+     * availability: HWC_DEVICE_API_VERSION_0_2
+     *
+     * Used to retrieve information about the h/w composer
+     *
+     * Returns 0 on success or -errno on error.
+     */
+    int (*query)(struct hwc_composer_device* dev, int what, int* value);
+
+    /*
+     * Reserved for future use. Must be NULL.
+     */
+    void* reserved_proc[4];
+
+    /*
+     * This field is OPTIONAL and can be NULL.
+     * availability: HWC_DEVICE_API_VERSION_0_3
+     */
+    hwc_methods_t const *methods;
+
+} hwc_composer_device_t;
+
+/** convenience API for opening and closing a device */
+
+static inline int hwc_open(const struct hw_module_t* module,
+        hwc_composer_device_t** device) {
+    return module->methods->open(module,
+            HWC_HARDWARE_COMPOSER, (struct hw_device_t**)device);
+}
+
+static inline int hwc_close(hwc_composer_device_t* device) {
+    return device->common.close(&device->common);
+}
+
+/*****************************************************************************/
+
+#endif /* ANDROID_INCLUDE_HARDWARE_HWCOMPOSER_V0_H */
diff --git a/include/hardware/keymaster.h b/include/hardware/keymaster.h
new file mode 100644
index 0000000..e0014c8
--- /dev/null
+++ b/include/hardware/keymaster.h
@@ -0,0 +1,214 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_HARDWARE_KEYMASTER_H
+#define ANDROID_HARDWARE_KEYMASTER_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+/**
+ * The id of this module
+ */
+#define KEYSTORE_HARDWARE_MODULE_ID "keystore"
+
+#define KEYSTORE_KEYMASTER "keymaster"
+
+/**
+ * The API level of this version of the header. The allows the implementing
+ * module to recognize which API level of the client it is dealing with in
+ * the case of pre-compiled binary clients.
+ */
+#define KEYMASTER_API_VERSION 1
+
+/**
+ * Flags for keymaster_device::flags
+ */
+enum {
+    /*
+     * Indicates this keymaster implementation does not have hardware that
+     * keeps private keys out of user space.
+     *
+     * This should not be implemented on anything other than the default
+     * implementation.
+     */
+    KEYMASTER_SOFTWARE_ONLY = 0x00000001,
+};
+
+struct keystore_module {
+    hw_module_t common;
+};
+
+/**
+ * Asymmetric key pair types.
+ */
+typedef enum {
+    TYPE_RSA = 1,
+} keymaster_keypair_t;
+
+/**
+ * Parameters needed to generate an RSA key.
+ */
+typedef struct {
+    uint32_t modulus_size;
+    uint64_t public_exponent;
+} keymaster_rsa_keygen_params_t;
+
+/**
+ * Digest type used for RSA operations.
+ */
+typedef enum {
+    DIGEST_NONE,
+} keymaster_rsa_digest_t;
+
+/**
+ * Type of padding used for RSA operations.
+ */
+typedef enum {
+    PADDING_NONE,
+} keymaster_rsa_padding_t;
+
+typedef struct {
+    keymaster_rsa_digest_t digest_type;
+    keymaster_rsa_padding_t padding_type;
+} keymaster_rsa_sign_params_t;
+
+/**
+ * The parameters that can be set for a given keymaster implementation.
+ */
+struct keymaster_device {
+    struct hw_device_t common;
+
+    uint32_t client_version;
+
+    /**
+     * See flags defined for keymaster_device::flags above.
+     */
+    uint32_t flags;
+
+    void* context;
+
+    /**
+     * Generates a public and private key. The key-blob returned is opaque
+     * and must subsequently provided for signing and verification.
+     *
+     * Returns: 0 on success or an error code less than 0.
+     */
+    int (*generate_keypair)(const struct keymaster_device* dev,
+            const keymaster_keypair_t key_type, const void* key_params,
+            uint8_t** key_blob, size_t* key_blob_length);
+
+    /**
+     * Imports a public and private key pair. The imported keys will be in
+     * PKCS#8 format with DER encoding (Java standard). The key-blob
+     * returned is opaque and will be subsequently provided for signing
+     * and verification.
+     *
+     * Returns: 0 on success or an error code less than 0.
+     */
+    int (*import_keypair)(const struct keymaster_device* dev,
+            const uint8_t* key, const size_t key_length,
+            uint8_t** key_blob, size_t* key_blob_length);
+
+    /**
+     * Gets the public key part of a key pair. The public key must be in
+     * X.509 format (Java standard) encoded byte array.
+     *
+     * Returns: 0 on success or an error code less than 0.
+     * On error, x509_data should not be allocated.
+     */
+    int (*get_keypair_public)(const struct keymaster_device* dev,
+            const uint8_t* key_blob, const size_t key_blob_length,
+            uint8_t** x509_data, size_t* x509_data_length);
+
+    /**
+     * Deletes the key pair associated with the key blob.
+     *
+     * This function is optional and should be set to NULL if it is not
+     * implemented.
+     *
+     * Returns 0 on success or an error code less than 0.
+     */
+    int (*delete_keypair)(const struct keymaster_device* dev,
+            const uint8_t* key_blob, const size_t key_blob_length);
+
+    /**
+     * Deletes all keys in the hardware keystore. Used when keystore is
+     * reset completely.
+     *
+     * This function is optional and should be set to NULL if it is not
+     * implemented.
+     *
+     * Returns 0 on success or an error code less than 0.
+     */
+    int (*delete_all)(const struct keymaster_device* dev);
+
+    /**
+     * Signs data using a key-blob generated before. This can use either
+     * an asymmetric key or a secret key.
+     *
+     * Returns: 0 on success or an error code less than 0.
+     */
+    int (*sign_data)(const struct keymaster_device* dev,
+            const void* signing_params,
+            const uint8_t* key_blob, const size_t key_blob_length,
+            const uint8_t* data, const size_t data_length,
+            uint8_t** signed_data, size_t* signed_data_length);
+
+    /**
+     * Verifies data signed with a key-blob. This can use either
+     * an asymmetric key or a secret key.
+     *
+     * Returns: 0 on successful verification or an error code less than 0.
+     */
+    int (*verify_data)(const struct keymaster_device* dev,
+            const void* signing_params,
+            const uint8_t* key_blob, const size_t key_blob_length,
+            const uint8_t* signed_data, const size_t signed_data_length,
+            const uint8_t* signature, const size_t signature_length);
+};
+typedef struct keymaster_device keymaster_device_t;
+
+
+/* Convenience API for opening and closing keymaster devices */
+
+static inline int keymaster_open(const struct hw_module_t* module,
+        keymaster_device_t** device)
+{
+    int rc = module->methods->open(module, KEYSTORE_KEYMASTER,
+            (struct hw_device_t**) device);
+
+    if (!rc) {
+        (*device)->client_version = KEYMASTER_API_VERSION;
+    }
+
+    return rc;
+}
+
+static inline int keymaster_close(keymaster_device_t* device)
+{
+    return device->common.close(&device->common);
+}
+
+__END_DECLS
+
+#endif  // ANDROID_HARDWARE_KEYMASTER_H
+
diff --git a/include/hardware/nfc.h b/include/hardware/nfc.h
index ca3683b..74b3cfb 100644
--- a/include/hardware/nfc.h
+++ b/include/hardware/nfc.h
@@ -76,6 +76,11 @@
      * Set to 0 to disable the workaround, 1 to enable it.
      */
     uint8_t enable_i2c_workaround;
+    /* I2C slave address. Multiple I2C addresses are
+     * possible for PN544 module. Configure address according to
+     * board design.
+     */
+    uint8_t i2c_device_address;
 } nfc_pn544_device_t;
 
 static inline int nfc_pn544_open(const struct hw_module_t* module,
diff --git a/include/hardware/power.h b/include/hardware/power.h
new file mode 100644
index 0000000..6c55061
--- /dev/null
+++ b/include/hardware/power.h
@@ -0,0 +1,123 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_INCLUDE_HARDWARE_POWER_H
+#define ANDROID_INCLUDE_HARDWARE_POWER_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+#define POWER_MODULE_API_VERSION_0_1  HARDWARE_MODULE_API_VERSION(0, 1)
+#define POWER_MODULE_API_VERSION_0_2  HARDWARE_MODULE_API_VERSION(0, 2)
+
+
+/**
+ * The id of this module
+ */
+#define POWER_HARDWARE_MODULE_ID "power"
+
+/*
+ * Power hint identifiers passed to (*powerHint)
+ */
+
+typedef enum {
+    POWER_HINT_VSYNC = 0x00000001,
+    POWER_HINT_INTERACTION = 0x00000002,
+} power_hint_t;
+
+/**
+ * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
+ * and the fields of this data structure must begin with hw_module_t
+ * followed by module specific information.
+ */
+typedef struct power_module {
+    struct hw_module_t common;
+
+    /*
+     * (*init)() performs power management setup actions at runtime
+     * startup, such as to set default cpufreq parameters.  This is
+     * called only by the Power HAL instance loaded by
+     * PowerManagerService.
+     */
+    void (*init)(struct power_module *module);
+
+    /*
+     * (*setInteractive)() performs power management actions upon the
+     * system entering interactive state (that is, the system is awake
+     * and ready for interaction, often with UI devices such as
+     * display and touchscreen enabled) or non-interactive state (the
+     * system appears asleep, display usually turned off).  The
+     * non-interactive state is usually entered after a period of
+     * inactivity, in order to conserve battery power during
+     * such inactive periods.
+     *
+     * Typical actions are to turn on or off devices and adjust
+     * cpufreq parameters.  This function may also call the
+     * appropriate interfaces to allow the kernel to suspend the
+     * system to low-power sleep state when entering non-interactive
+     * state, and to disallow low-power suspend when the system is in
+     * interactive state.  When low-power suspend state is allowed, the
+     * kernel may suspend the system whenever no wakelocks are held.
+     *
+     * on is non-zero when the system is transitioning to an
+     * interactive / awake state, and zero when transitioning to a
+     * non-interactive / asleep state.
+     *
+     * This function is called to enter non-interactive state after
+     * turning off the screen (if present), and called to enter
+     * interactive state prior to turning on the screen.
+     */
+    void (*setInteractive)(struct power_module *module, int on);
+
+    /*
+     * (*powerHint) is called to pass hints on power requirements, which
+     * may result in adjustment of power/performance parameters of the
+     * cpufreq governor and other controls.  The possible hints are:
+     *
+     * POWER_HINT_VSYNC
+     *
+     *     Foreground app has started or stopped requesting a VSYNC pulse
+     *     from SurfaceFlinger.  If the app has started requesting VSYNC
+     *     then CPU and GPU load is expected soon, and it may be appropriate
+     *     to raise speeds of CPU, memory bus, etc.  The data parameter is
+     *     non-zero to indicate VSYNC pulse is now requested, or zero for
+     *     VSYNC pulse no longer requested.
+     *
+     * POWER_HINT_INTERACTION
+     *
+     *     User is interacting with the device, for example, touchscreen
+     *     events are incoming.  CPU and GPU load may be expected soon,
+     *     and it may be appropriate to raise speeds of CPU, memory bus,
+     *     etc.  The data parameter is unused.
+     *
+     * A particular platform may choose to ignore any hint.
+     *
+     * availability: version 0.2
+     *
+     */
+    void (*powerHint)(struct power_module *module, power_hint_t hint,
+                      void *data);
+} power_module_t;
+
+
+__END_DECLS
+
+#endif  // ANDROID_INCLUDE_HARDWARE_POWER_H
diff --git a/modules/Android.mk b/modules/Android.mk
index cea1bb3..871b984 100644
--- a/modules/Android.mk
+++ b/modules/Android.mk
@@ -1,2 +1,2 @@
-hardware_modules := gralloc hwcomposer audio local_time nfc
+hardware_modules := gralloc hwcomposer audio nfc local_time power usbaudio
 include $(call all-named-subdir-makefiles,$(hardware_modules))
diff --git a/modules/audio/audio_hw.c b/modules/audio/audio_hw.c
index 47b3bb4..15e9920 100644
--- a/modules/audio/audio_hw.c
+++ b/modules/audio/audio_hw.c
@@ -55,17 +55,17 @@
     return 4096;
 }
 
-static uint32_t out_get_channels(const struct audio_stream *stream)
+static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
 {
     return AUDIO_CHANNEL_OUT_STEREO;
 }
 
-static int out_get_format(const struct audio_stream *stream)
+static audio_format_t out_get_format(const struct audio_stream *stream)
 {
     return AUDIO_FORMAT_PCM_16_BIT;
 }
 
-static int out_set_format(struct audio_stream *stream, int format)
+static int out_set_format(struct audio_stream *stream, audio_format_t format)
 {
     return 0;
 }
@@ -148,17 +148,17 @@
     return 320;
 }
 
-static uint32_t in_get_channels(const struct audio_stream *stream)
+static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
 {
     return AUDIO_CHANNEL_IN_MONO;
 }
 
-static int in_get_format(const struct audio_stream *stream)
+static audio_format_t in_get_format(const struct audio_stream *stream)
 {
     return AUDIO_FORMAT_PCM_16_BIT;
 }
 
-static int in_set_format(struct audio_stream *stream, int format)
+static int in_set_format(struct audio_stream *stream, audio_format_t format)
 {
     return 0;
 }
@@ -214,8 +214,10 @@
 }
 
 static int adev_open_output_stream(struct audio_hw_device *dev,
-                                   uint32_t devices, int *format,
-                                   uint32_t *channels, uint32_t *sample_rate,
+                                   audio_io_handle_t handle,
+                                   audio_devices_t devices,
+                                   audio_output_flags_t flags,
+                                   struct audio_config *config,
                                    struct audio_stream_out **stream_out)
 {
     struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
@@ -285,22 +287,13 @@
     return -ENOSYS;
 }
 
-static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
+static int adev_get_master_volume(struct audio_hw_device *dev,
+                                  float *volume)
 {
     return -ENOSYS;
 }
 
-static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
-{
-    return -ENOSYS;
-}
-
-static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
-{
-    return -ENOSYS;
-}
-
-static int adev_set_mode(struct audio_hw_device *dev, int mode)
+static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
 {
     return 0;
 }
@@ -316,16 +309,15 @@
 }
 
 static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
-                                         uint32_t sample_rate, int format,
-                                         int channel_count)
+                                         const struct audio_config *config)
 {
     return 320;
 }
 
-static int adev_open_input_stream(struct audio_hw_device *dev, uint32_t devices,
-                                  int *format, uint32_t *channels,
-                                  uint32_t *sample_rate,
-                                  audio_in_acoustics_t acoustics,
+static int adev_open_input_stream(struct audio_hw_device *dev,
+                                  audio_io_handle_t handle,
+                                  audio_devices_t devices,
+                                  struct audio_config *config,
                                   struct audio_stream_in **stream_in)
 {
     struct stub_audio_device *ladev = (struct stub_audio_device *)dev;
@@ -415,7 +407,7 @@
         return -ENOMEM;
 
     adev->device.common.tag = HARDWARE_DEVICE_TAG;
-    adev->device.common.version = 0;
+    adev->device.common.version = AUDIO_DEVICE_API_VERSION_1_0;
     adev->device.common.module = (struct hw_module_t *) module;
     adev->device.common.close = adev_close;
 
@@ -424,8 +416,6 @@
     adev->device.set_voice_volume = adev_set_voice_volume;
     adev->device.set_master_volume = adev_set_master_volume;
     adev->device.get_master_volume = adev_get_master_volume;
-    adev->device.set_master_mute = adev_set_master_mute;
-    adev->device.get_master_mute = adev_get_master_mute;
     adev->device.set_mode = adev_set_mode;
     adev->device.set_mic_mute = adev_set_mic_mute;
     adev->device.get_mic_mute = adev_get_mic_mute;
@@ -450,8 +440,8 @@
 struct audio_module HAL_MODULE_INFO_SYM = {
     .common = {
         .tag = HARDWARE_MODULE_TAG,
-        .version_major = 1,
-        .version_minor = 0,
+        .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
+        .hal_api_version = HARDWARE_HAL_API_VERSION,
         .id = AUDIO_HARDWARE_MODULE_ID,
         .name = "Default audio HW HAL",
         .author = "The Android Open Source Project",
diff --git a/modules/audio/audio_policy.c b/modules/audio/audio_policy.c
index 409f0a1..a44ea77 100644
--- a/modules/audio/audio_policy.c
+++ b/modules/audio/audio_policy.c
@@ -58,10 +58,11 @@
     return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
 }
 
-static void ap_set_phone_state(struct audio_policy *pol, int state)
+static void ap_set_phone_state(struct audio_policy *pol, audio_mode_t state)
 {
 }
 
+// deprecated, never called
 static void ap_set_ringer_mode(struct audio_policy *pol, uint32_t mode,
                                uint32_t mask)
 {
@@ -96,9 +97,9 @@
 static audio_io_handle_t ap_get_output(struct audio_policy *pol,
                                        audio_stream_type_t stream,
                                        uint32_t sampling_rate,
-                                       uint32_t format,
-                                       uint32_t channels,
-                                       audio_policy_output_flags_t flags)
+                                       audio_format_t format,
+                                       audio_channel_mask_t channelMask,
+                                       audio_output_flags_t flags)
 {
     return 0;
 }
@@ -120,10 +121,10 @@
 {
 }
 
-static audio_io_handle_t ap_get_input(struct audio_policy *pol, int inputSource,
+static audio_io_handle_t ap_get_input(struct audio_policy *pol, audio_source_t inputSource,
                                       uint32_t sampling_rate,
-                                      uint32_t format,
-                                      uint32_t channels,
+                                      audio_format_t format,
+                                      audio_channel_mask_t channelMask,
                                       audio_in_acoustics_t acoustics)
 {
     return 0;
@@ -163,13 +164,29 @@
     return -ENOSYS;
 }
 
+static int ap_set_stream_volume_index_for_device(struct audio_policy *pol,
+                                      audio_stream_type_t stream,
+                                      int index,
+                                      audio_devices_t device)
+{
+    return -ENOSYS;
+}
+
+static int ap_get_stream_volume_index_for_device(const struct audio_policy *pol,
+                                      audio_stream_type_t stream,
+                                      int *index,
+                                      audio_devices_t device)
+{
+    return -ENOSYS;
+}
+
 static uint32_t ap_get_strategy_for_stream(const struct audio_policy *pol,
                                            audio_stream_type_t stream)
 {
     return 0;
 }
 
-static uint32_t ap_get_devices_for_stream(const struct audio_policy *pol,
+static audio_devices_t ap_get_devices_for_stream(const struct audio_policy *pol,
                                           audio_stream_type_t stream)
 {
     return 0;
@@ -201,7 +218,7 @@
     return -ENOSYS;
 }
 
-static bool ap_is_stream_active(const struct audio_policy *pol, int stream,
+static bool ap_is_stream_active(const struct audio_policy *pol, audio_stream_type_t stream,
                                 uint32_t in_past_ms)
 {
     return false;
@@ -250,6 +267,8 @@
     dap->policy.init_stream_volume = ap_init_stream_volume;
     dap->policy.set_stream_volume_index = ap_set_stream_volume_index;
     dap->policy.get_stream_volume_index = ap_get_stream_volume_index;
+    dap->policy.set_stream_volume_index_for_device = ap_set_stream_volume_index_for_device;
+    dap->policy.get_stream_volume_index_for_device = ap_get_stream_volume_index_for_device;
     dap->policy.get_strategy_for_stream = ap_get_strategy_for_stream;
     dap->policy.get_devices_for_stream = ap_get_devices_for_stream;
     dap->policy.get_output_for_effect = ap_get_output_for_effect;
diff --git a/modules/gralloc/framebuffer.cpp b/modules/gralloc/framebuffer.cpp
index f908976..326f2ae 100644
--- a/modules/gralloc/framebuffer.cpp
+++ b/modules/gralloc/framebuffer.cpp
@@ -98,7 +98,7 @@
         m->info.activate = FB_ACTIVATE_VBL;
         m->info.yoffset = offset / m->finfo.line_length;
         if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
-            LOGE("FBIOPUT_VSCREENINFO failed");
+            ALOGE("FBIOPUT_VSCREENINFO failed");
             m->base.unlock(&m->base, buffer); 
             return -errno;
         }
@@ -181,14 +181,14 @@
     if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
         info.yres_virtual = info.yres;
         flags &= ~PAGE_FLIP;
-        LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
+        ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
     }
 
     if (info.yres_virtual < info.yres * 2) {
         // we need at least 2 for page-flipping
         info.yres_virtual = info.yres;
         flags &= ~PAGE_FLIP;
-        LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
+        ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
                 info.yres_virtual, info.yres*2);
     }
 
@@ -222,7 +222,7 @@
     float ydpi = (info.yres * 25.4f) / info.height;
     float fps  = refreshRate / 1000.0f;
 
-    LOGI(   "using (fd=%d)\n"
+    ALOGI(   "using (fd=%d)\n"
             "id           = %s\n"
             "xres         = %d px\n"
             "yres         = %d px\n"
@@ -244,7 +244,7 @@
             info.blue.offset, info.blue.length
     );
 
-    LOGI(   "width        = %d mm (%f dpi)\n"
+    ALOGI(   "width        = %d mm (%f dpi)\n"
             "height       = %d mm (%f dpi)\n"
             "refresh rate = %.2f Hz\n",
             info.width,  xdpi,
@@ -280,7 +280,7 @@
 
     void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
     if (vaddr == MAP_FAILED) {
-        LOGE("Error mapping the framebuffer (%s)", strerror(errno));
+        ALOGE("Error mapping the framebuffer (%s)", strerror(errno));
         return -errno;
     }
     module->framebuffer->base = intptr_t(vaddr);
@@ -312,11 +312,6 @@
 {
     int status = -EINVAL;
     if (!strcmp(name, GRALLOC_HARDWARE_FB0)) {
-        alloc_device_t* gralloc_device;
-        status = gralloc_open(module, &gralloc_device);
-        if (status < 0)
-            return status;
-
         /* initialize our state here */
         fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev));
         memset(dev, 0, sizeof(*dev));
diff --git a/modules/gralloc/gralloc.cpp b/modules/gralloc/gralloc.cpp
index dcd2e2b..99aeb01 100644
--- a/modules/gralloc/gralloc.cpp
+++ b/modules/gralloc/gralloc.cpp
@@ -175,7 +175,7 @@
     
     fd = ashmem_create_region("gralloc-buffer", size);
     if (fd < 0) {
-        LOGE("couldn't create ashmem (%s)", strerror(-errno));
+        ALOGE("couldn't create ashmem (%s)", strerror(-errno));
         err = -errno;
     }
 
@@ -189,7 +189,7 @@
         }
     }
     
-    LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
+    ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
     
     return err;
 }
@@ -219,6 +219,7 @@
         case HAL_PIXEL_FORMAT_RGB_565:
         case HAL_PIXEL_FORMAT_RGBA_5551:
         case HAL_PIXEL_FORMAT_RGBA_4444:
+        case HAL_PIXEL_FORMAT_RAW_SENSOR:
             bpp = 2;
             break;
         default:
diff --git a/modules/gralloc/gralloc_priv.h b/modules/gralloc/gralloc_priv.h
index 75bcd1d..e1c502a 100644
--- a/modules/gralloc/gralloc_priv.h
+++ b/modules/gralloc/gralloc_priv.h
@@ -101,7 +101,7 @@
                 h->numInts != sNumInts || h->numFds != sNumFds ||
                 hnd->magic != sMagic) 
         {
-            LOGE("invalid gralloc handle (at %p)", h);
+            ALOGE("invalid gralloc handle (at %p)", h);
             return -EINVAL;
         }
         return 0;
diff --git a/modules/gralloc/mapper.cpp b/modules/gralloc/mapper.cpp
index 33d0958..c4096ae 100644
--- a/modules/gralloc/mapper.cpp
+++ b/modules/gralloc/mapper.cpp
@@ -53,11 +53,11 @@
         void* mappedAddress = mmap(0, size,
                 PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0);
         if (mappedAddress == MAP_FAILED) {
-            LOGE("Could not mmap %s", strerror(errno));
+            ALOGE("Could not mmap %s", strerror(errno));
             return -errno;
         }
         hnd->base = intptr_t(mappedAddress) + hnd->offset;
-        //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
+        //ALOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
         //        hnd->fd, hnd->offset, hnd->size, mappedAddress);
     }
     *vaddr = (void*)hnd->base;
@@ -71,9 +71,9 @@
     if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
         void* base = (void*)hnd->base;
         size_t size = hnd->size;
-        //LOGD("unmapping from %p, size=%d", base, size);
+        //ALOGD("unmapping from %p, size=%d", base, size);
         if (munmap(base, size) < 0) {
-            LOGE("Could not unmap %s", strerror(errno));
+            ALOGE("Could not unmap %s", strerror(errno));
         }
     }
     hnd->base = 0;
diff --git a/modules/hwcomposer/hwcomposer.cpp b/modules/hwcomposer/hwcomposer.cpp
index 68b7070..0e49e4c 100644
--- a/modules/hwcomposer/hwcomposer.cpp
+++ b/modules/hwcomposer/hwcomposer.cpp
@@ -29,7 +29,7 @@
 /*****************************************************************************/
 
 struct hwc_context_t {
-    hwc_composer_device_t device;
+    hwc_composer_device_1_t device;
     /* our private state goes below here */
 };
 
@@ -54,8 +54,8 @@
 
 /*****************************************************************************/
 
-static void dump_layer(hwc_layer_t const* l) {
-    LOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
+static void dump_layer(hwc_layer_1_t const* l) {
+    ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
             l->compositionType, l->flags, l->handle, l->transform, l->blending,
             l->sourceCrop.left,
             l->sourceCrop.top,
@@ -67,7 +67,7 @@
             l->displayFrame.bottom);
 }
 
-static int hwc_prepare(hwc_composer_device_t *dev, hwc_layer_list_t* list) {
+static int hwc_prepare(hwc_composer_device_1_t *dev, hwc_layer_list_1_t* list) {
     if (list && (list->flags & HWC_GEOMETRY_CHANGED)) {
         for (size_t i=0 ; i<list->numHwLayers ; i++) {
             //dump_layer(&list->hwLayers[i]);
@@ -77,10 +77,10 @@
     return 0;
 }
 
-static int hwc_set(hwc_composer_device_t *dev,
+static int hwc_set(hwc_composer_device_1_t *dev,
         hwc_display_t dpy,
         hwc_surface_t sur,
-        hwc_layer_list_t* list)
+        hwc_layer_list_1_t* list)
 {
     //for (size_t i=0 ; i<list->numHwLayers ; i++) {
     //    dump_layer(&list->hwLayers[i]);
@@ -117,7 +117,7 @@
 
         /* initialize the procs */
         dev->device.common.tag = HARDWARE_DEVICE_TAG;
-        dev->device.common.version = 0;
+        dev->device.common.version = HWC_DEVICE_API_VERSION_1_0;
         dev->device.common.module = const_cast<hw_module_t*>(module);
         dev->device.common.close = hwc_device_close;
 
diff --git a/modules/local_time/local_time_hw.c b/modules/local_time/local_time_hw.c
index a1f5d43..308f7d9 100644
--- a/modules/local_time/local_time_hw.c
+++ b/modules/local_time/local_time_hw.c
@@ -39,7 +39,7 @@
 
     ret = clock_gettime(CLOCK_MONOTONIC, &ts);
     if (ret < 0) {
-        LOGW("%s failed to fetch CLOCK_MONOTONIC value! (res = %d)",
+        ALOGW("%s failed to fetch CLOCK_MONOTONIC value! (res = %d)",
                 dev->common.module->name, ret);
         return 0;
     }
diff --git a/modules/nfc/nfc_hw_example.c b/modules/nfc/nfc_hw_example.c
index 9769282..54c9c56 100644
--- a/modules/nfc/nfc_hw_example.c
+++ b/modules/nfc/nfc_hw_example.c
@@ -44,6 +44,7 @@
         dev->linktype = PN544_LINK_TYPE_INVALID;
         dev->device_node = NULL;
         dev->enable_i2c_workaround = 0;
+        dev->i2c_device_address = 0;
 
         *device = (hw_device_t*) dev;
         return 0;
diff --git a/modules/power/Android.mk b/modules/power/Android.mk
new file mode 100644
index 0000000..50ab221
--- /dev/null
+++ b/modules/power/Android.mk
@@ -0,0 +1,25 @@
+# Copyright (C) 2011 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := power.default
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+LOCAL_SRC_FILES := power.c
+LOCAL_SHARED_LIBRARIES := liblog
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/modules/power/power.c b/modules/power/power.c
new file mode 100644
index 0000000..7cacc09
--- /dev/null
+++ b/modules/power/power.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <errno.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#define LOG_TAG "Legacy PowerHAL"
+#include <utils/Log.h>
+
+#include <hardware/hardware.h>
+#include <hardware/power.h>
+
+static void power_init(struct power_module *module)
+{
+}
+
+static void power_set_interactive(struct power_module *module, int on)
+{
+}
+
+static void power_hint(struct power_module *module, power_hint_t hint,
+                       void *data) {
+    switch (hint) {
+    default:
+        break;
+    }
+}
+
+static struct hw_module_methods_t power_module_methods = {
+    .open = NULL,
+};
+
+struct power_module HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag = HARDWARE_MODULE_TAG,
+        .module_api_version = POWER_MODULE_API_VERSION_0_2,
+        .hal_api_version = HARDWARE_HAL_API_VERSION,
+        .id = POWER_HARDWARE_MODULE_ID,
+        .name = "Default Power HAL",
+        .author = "The Android Open Source Project",
+        .methods = &power_module_methods,
+    },
+
+    .init = power_init,
+    .setInteractive = power_set_interactive,
+    .powerHint = power_hint,
+};
diff --git a/modules/usbaudio/Android.mk b/modules/usbaudio/Android.mk
new file mode 100644
index 0000000..6b5e66d
--- /dev/null
+++ b/modules/usbaudio/Android.mk
@@ -0,0 +1,29 @@
+# Copyright (C) 2012 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := audio.usb.default
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+LOCAL_SRC_FILES := \
+	audio_hw.c
+LOCAL_C_INCLUDES += \
+	external/tinyalsa/include
+LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa
+LOCAL_MODULE_TAGS := optional
+
+include $(BUILD_SHARED_LIBRARY)
+
diff --git a/modules/usbaudio/audio_hw.c b/modules/usbaudio/audio_hw.c
new file mode 100644
index 0000000..9283016
--- /dev/null
+++ b/modules/usbaudio/audio_hw.c
@@ -0,0 +1,440 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "usb_audio_hw"
+/*#define LOG_NDEBUG 0*/
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdint.h>
+#include <sys/time.h>
+#include <stdlib.h>
+
+#include <cutils/log.h>
+#include <cutils/str_parms.h>
+#include <cutils/properties.h>
+
+#include <hardware/hardware.h>
+#include <system/audio.h>
+#include <hardware/audio.h>
+
+#include <tinyalsa/asoundlib.h>
+
+struct pcm_config pcm_config = {
+    .channels = 2,
+    .rate = 44100,
+    .period_size = 1024,
+    .period_count = 4,
+    .format = PCM_FORMAT_S16_LE,
+};
+
+struct audio_device {
+    struct audio_hw_device hw_device;
+
+    pthread_mutex_t lock; /* see note below on mutex acquisition order */
+    int card;
+    int device;
+    bool standby;
+};
+
+struct stream_out {
+    struct audio_stream_out stream;
+
+    pthread_mutex_t lock; /* see note below on mutex acquisition order */
+    struct pcm *pcm;
+    bool standby;
+
+    struct audio_device *dev;
+};
+
+/**
+ * NOTE: when multiple mutexes have to be acquired, always respect the
+ * following order: hw device > out stream
+ */
+
+/* Helper functions */
+
+/* must be called with hw device and output stream mutexes locked */
+static int start_output_stream(struct stream_out *out)
+{
+    struct audio_device *adev = out->dev;
+    int i;
+
+    if ((adev->card < 0) || (adev->device < 0))
+        return -EINVAL;
+
+    out->pcm = pcm_open(adev->card, adev->device, PCM_OUT, &pcm_config);
+
+    if (out->pcm && !pcm_is_ready(out->pcm)) {
+        ALOGE("pcm_open() failed: %s", pcm_get_error(out->pcm));
+        pcm_close(out->pcm);
+        return -ENOMEM;
+    }
+
+    return 0;
+}
+
+/* API functions */
+
+static uint32_t out_get_sample_rate(const struct audio_stream *stream)
+{
+    return pcm_config.rate;
+}
+
+static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
+{
+    return 0;
+}
+
+static size_t out_get_buffer_size(const struct audio_stream *stream)
+{
+    return pcm_config.period_size *
+           audio_stream_frame_size((struct audio_stream *)stream);
+}
+
+static uint32_t out_get_channels(const struct audio_stream *stream)
+{
+    return AUDIO_CHANNEL_OUT_STEREO;
+}
+
+static audio_format_t out_get_format(const struct audio_stream *stream)
+{
+    return AUDIO_FORMAT_PCM_16_BIT;
+}
+
+static int out_set_format(struct audio_stream *stream, audio_format_t format)
+{
+    return 0;
+}
+
+static int out_standby(struct audio_stream *stream)
+{
+    struct stream_out *out = (struct stream_out *)stream;
+
+    pthread_mutex_lock(&out->dev->lock);
+    pthread_mutex_lock(&out->lock);
+
+    if (!out->standby) {
+        pcm_close(out->pcm);
+        out->pcm = NULL;
+        out->standby = true;
+    }
+
+    pthread_mutex_unlock(&out->lock);
+    pthread_mutex_unlock(&out->dev->lock);
+
+    return 0;
+}
+
+static int out_dump(const struct audio_stream *stream, int fd)
+{
+    return 0;
+}
+
+static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
+{
+    struct stream_out *out = (struct stream_out *)stream;
+    struct audio_device *adev = out->dev;
+    struct str_parms *parms;
+    char value[32];
+    int ret;
+    int routing = 0;
+
+    parms = str_parms_create_str(kvpairs);
+    pthread_mutex_lock(&adev->lock);
+
+    ret = str_parms_get_str(parms, "card", value, sizeof(value));
+    if (ret >= 0)
+        adev->card = atoi(value);
+
+    ret = str_parms_get_str(parms, "device", value, sizeof(value));
+    if (ret >= 0)
+        adev->device = atoi(value);
+
+    pthread_mutex_unlock(&adev->lock);
+    str_parms_destroy(parms);
+
+    return 0;
+}
+
+static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
+{
+    return strdup("");
+}
+
+static uint32_t out_get_latency(const struct audio_stream_out *stream)
+{
+    return (pcm_config.period_size * pcm_config.period_count * 1000) /
+            out_get_sample_rate(&stream->common);
+}
+
+static int out_set_volume(struct audio_stream_out *stream, float left,
+                          float right)
+{
+    return -ENOSYS;
+}
+
+static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
+                         size_t bytes)
+{
+    int ret;
+    struct stream_out *out = (struct stream_out *)stream;
+
+    pthread_mutex_lock(&out->dev->lock);
+    pthread_mutex_lock(&out->lock);
+    if (out->standby) {
+        ret = start_output_stream(out);
+        if (ret != 0) {
+            goto err;
+        }
+        out->standby = false;
+    }
+
+    pcm_write(out->pcm, (void *)buffer, bytes);
+
+    pthread_mutex_unlock(&out->lock);
+    pthread_mutex_unlock(&out->dev->lock);
+
+    return bytes;
+
+err:
+    pthread_mutex_unlock(&out->lock);
+
+    if (ret != 0) {
+        usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) /
+               out_get_sample_rate(&stream->common));
+    }
+
+    return bytes;
+}
+
+static int out_get_render_position(const struct audio_stream_out *stream,
+                                   uint32_t *dsp_frames)
+{
+    return -EINVAL;
+}
+
+static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
+{
+    return 0;
+}
+
+static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
+{
+    return 0;
+}
+
+static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
+                                        int64_t *timestamp)
+{
+    return -EINVAL;
+}
+
+static int adev_open_output_stream(struct audio_hw_device *dev,
+                                   audio_io_handle_t handle,
+                                   audio_devices_t devices,
+                                   audio_output_flags_t flags,
+                                   struct audio_config *config,
+                                   struct audio_stream_out **stream_out)
+{
+    struct audio_device *adev = (struct audio_device *)dev;
+    struct stream_out *out;
+    int ret;
+
+    out = (struct stream_out *)calloc(1, sizeof(struct stream_out));
+    if (!out)
+        return -ENOMEM;
+
+    out->stream.common.get_sample_rate = out_get_sample_rate;
+    out->stream.common.set_sample_rate = out_set_sample_rate;
+    out->stream.common.get_buffer_size = out_get_buffer_size;
+    out->stream.common.get_channels = out_get_channels;
+    out->stream.common.get_format = out_get_format;
+    out->stream.common.set_format = out_set_format;
+    out->stream.common.standby = out_standby;
+    out->stream.common.dump = out_dump;
+    out->stream.common.set_parameters = out_set_parameters;
+    out->stream.common.get_parameters = out_get_parameters;
+    out->stream.common.add_audio_effect = out_add_audio_effect;
+    out->stream.common.remove_audio_effect = out_remove_audio_effect;
+    out->stream.get_latency = out_get_latency;
+    out->stream.set_volume = out_set_volume;
+    out->stream.write = out_write;
+    out->stream.get_render_position = out_get_render_position;
+    out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
+
+    out->dev = adev;
+
+    config->format = out_get_format(&out->stream.common);
+    config->channel_mask = out_get_channels(&out->stream.common);
+    config->sample_rate = out_get_sample_rate(&out->stream.common);
+
+    out->standby = true;
+
+    adev->card = -1;
+    adev->device = -1;
+
+    *stream_out = &out->stream;
+    return 0;
+
+err_open:
+    free(out);
+    *stream_out = NULL;
+    return ret;
+}
+
+static void adev_close_output_stream(struct audio_hw_device *dev,
+                                     struct audio_stream_out *stream)
+{
+    struct stream_out *out = (struct stream_out *)stream;
+
+    out_standby(&stream->common);
+    free(stream);
+}
+
+static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
+{
+    return 0;
+}
+
+static char * adev_get_parameters(const struct audio_hw_device *dev,
+                                  const char *keys)
+{
+    return strdup("");
+}
+
+static int adev_init_check(const struct audio_hw_device *dev)
+{
+    return 0;
+}
+
+static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
+{
+    return -ENOSYS;
+}
+
+static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
+{
+    return -ENOSYS;
+}
+
+static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
+{
+    return 0;
+}
+
+static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
+{
+    return -ENOSYS;
+}
+
+static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
+{
+    return -ENOSYS;
+}
+
+static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
+                                         const struct audio_config *config)
+{
+    return 0;
+}
+
+static int adev_open_input_stream(struct audio_hw_device *dev,
+                                  audio_io_handle_t handle,
+                                  audio_devices_t devices,
+                                  struct audio_config *config,
+                                  struct audio_stream_in **stream_in)
+{
+    return -ENOSYS;
+}
+
+static void adev_close_input_stream(struct audio_hw_device *dev,
+                                   struct audio_stream_in *stream)
+{
+}
+
+static int adev_dump(const audio_hw_device_t *device, int fd)
+{
+    return 0;
+}
+
+static int adev_close(hw_device_t *device)
+{
+    struct audio_device *adev = (struct audio_device *)device;
+
+    free(device);
+    return 0;
+}
+
+static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev)
+{
+    return AUDIO_DEVICE_OUT_ALL_USB;
+}
+
+static int adev_open(const hw_module_t* module, const char* name,
+                     hw_device_t** device)
+{
+    struct audio_device *adev;
+    int ret;
+
+    if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
+        return -EINVAL;
+
+    adev = calloc(1, sizeof(struct audio_device));
+    if (!adev)
+        return -ENOMEM;
+
+    adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
+    adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_1_0;
+    adev->hw_device.common.module = (struct hw_module_t *) module;
+    adev->hw_device.common.close = adev_close;
+
+    adev->hw_device.get_supported_devices = adev_get_supported_devices;
+    adev->hw_device.init_check = adev_init_check;
+    adev->hw_device.set_voice_volume = adev_set_voice_volume;
+    adev->hw_device.set_master_volume = adev_set_master_volume;
+    adev->hw_device.set_mode = adev_set_mode;
+    adev->hw_device.set_mic_mute = adev_set_mic_mute;
+    adev->hw_device.get_mic_mute = adev_get_mic_mute;
+    adev->hw_device.set_parameters = adev_set_parameters;
+    adev->hw_device.get_parameters = adev_get_parameters;
+    adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
+    adev->hw_device.open_output_stream = adev_open_output_stream;
+    adev->hw_device.close_output_stream = adev_close_output_stream;
+    adev->hw_device.open_input_stream = adev_open_input_stream;
+    adev->hw_device.close_input_stream = adev_close_input_stream;
+    adev->hw_device.dump = adev_dump;
+
+    *device = &adev->hw_device.common;
+
+    return 0;
+}
+
+static struct hw_module_methods_t hal_module_methods = {
+    .open = adev_open,
+};
+
+struct audio_module HAL_MODULE_INFO_SYM = {
+    .common = {
+        .tag = HARDWARE_MODULE_TAG,
+        .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
+        .hal_api_version = HARDWARE_HAL_API_VERSION,
+        .id = AUDIO_HARDWARE_MODULE_ID,
+        .name = "USB audio HW HAL",
+        .author = "The Android Open Source Project",
+        .methods = &hal_module_methods,
+    },
+};
diff --git a/tests/camera2/Android.mk b/tests/camera2/Android.mk
new file mode 100644
index 0000000..c378e12
--- /dev/null
+++ b/tests/camera2/Android.mk
@@ -0,0 +1,31 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+	camera2.cpp \
+	camera2_utils.cpp
+
+LOCAL_SHARED_LIBRARIES := \
+	libutils \
+	libstlport \
+	libhardware \
+	libcamera_metadata \
+	libgui \
+	libsync \
+	libui
+
+LOCAL_STATIC_LIBRARIES := \
+	libgtest \
+	libgtest_main
+
+LOCAL_C_INCLUDES += \
+	bionic \
+	bionic/libstdc++/include \
+	external/gtest/include \
+	external/stlport/stlport \
+	system/media/camera/include \
+
+LOCAL_MODULE:= camera2_test
+LOCAL_MODULE_TAGS := tests
+
+include $(BUILD_EXECUTABLE)
diff --git a/tests/camera2/camera2.cpp b/tests/camera2/camera2.cpp
new file mode 100644
index 0000000..8a0e842
--- /dev/null
+++ b/tests/camera2/camera2.cpp
@@ -0,0 +1,724 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "Camera2_test"
+#define LOG_NDEBUG 0
+
+#include <utils/Log.h>
+#include <gtest/gtest.h>
+#include <iostream>
+#include <fstream>
+
+#include <utils/Vector.h>
+#include <gui/CpuConsumer.h>
+#include <ui/PixelFormat.h>
+#include <system/camera_metadata.h>
+
+#include "camera2_utils.h"
+
+namespace android {
+
+class Camera2Test: public testing::Test {
+  public:
+    static void SetUpTestCase() {
+        int res;
+
+        hw_module_t *module = NULL;
+        res = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
+                (const hw_module_t **)&module);
+
+        ASSERT_EQ(0, res)
+                << "Failure opening camera hardware module: " << res;
+        ASSERT_TRUE(NULL != module)
+                << "No camera module was set by hw_get_module";
+
+        IF_ALOGV() {
+            std::cout << "  Camera module name: "
+                    << module->name << std::endl;
+            std::cout << "  Camera module author: "
+                    << module->author << std::endl;
+            std::cout << "  Camera module API version: 0x" << std::hex
+                    << module->module_api_version << std::endl;
+            std::cout << "  Camera module HAL API version: 0x" << std::hex
+                    << module->hal_api_version << std::endl;
+        }
+
+        int16_t version2_0 = CAMERA_MODULE_API_VERSION_2_0;
+        ASSERT_EQ(version2_0, module->module_api_version)
+                << "Camera module version is 0x"
+                << std::hex << module->module_api_version
+                << ", not 2.0. (0x"
+                << std::hex << CAMERA_MODULE_API_VERSION_2_0 << ")";
+
+        sCameraModule = reinterpret_cast<camera_module_t*>(module);
+
+        sNumCameras = sCameraModule->get_number_of_cameras();
+        ASSERT_LT(0, sNumCameras) << "No camera devices available!";
+
+        IF_ALOGV() {
+            std::cout << "  Camera device count: " << sNumCameras << std::endl;
+        }
+
+        sCameraSupportsHal2 = new bool[sNumCameras];
+
+        for (int i = 0; i < sNumCameras; i++) {
+            camera_info info;
+            res = sCameraModule->get_camera_info(i, &info);
+            ASSERT_EQ(0, res)
+                    << "Failure getting camera info for camera " << i;
+            IF_ALOGV() {
+                std::cout << "  Camera device: " << std::dec
+                          << i << std::endl;;
+                std::cout << "    Facing: " << std::dec
+                          << info.facing  << std::endl;
+                std::cout << "    Orientation: " << std::dec
+                          << info.orientation  << std::endl;
+                std::cout << "    Version: 0x" << std::hex <<
+                        info.device_version  << std::endl;
+            }
+            if (info.device_version >= CAMERA_DEVICE_API_VERSION_2_0) {
+                sCameraSupportsHal2[i] = true;
+                ASSERT_TRUE(NULL != info.static_camera_characteristics);
+                IF_ALOGV() {
+                    std::cout << "    Static camera metadata:"  << std::endl;
+                    dump_camera_metadata(info.static_camera_characteristics,
+                            0, 1);
+                }
+            } else {
+                sCameraSupportsHal2[i] = false;
+            }
+        }
+    }
+
+    static const camera_module_t *getCameraModule() {
+        return sCameraModule;
+    }
+
+    static int getNumCameras() {
+        return sNumCameras;
+    }
+
+    static bool isHal2Supported(int id) {
+        return sCameraSupportsHal2[id];
+    }
+
+    static camera2_device_t *openCameraDevice(int id) {
+        ALOGV("Opening camera %d", id);
+        if (NULL == sCameraSupportsHal2) return NULL;
+        if (id >= sNumCameras) return NULL;
+        if (!sCameraSupportsHal2[id]) return NULL;
+
+        hw_device_t *device = NULL;
+        const camera_module_t *cam_module = getCameraModule();
+        if (cam_module == NULL) {
+            return NULL;
+        }
+
+        char camId[10];
+        int res;
+
+        snprintf(camId, 10, "%d", id);
+        res = cam_module->common.methods->open(
+            (const hw_module_t*)cam_module,
+            camId,
+            &device);
+        if (res != NO_ERROR || device == NULL) {
+            return NULL;
+        }
+        camera2_device_t *cam_device =
+                reinterpret_cast<camera2_device_t*>(device);
+        return cam_device;
+    }
+
+    static status_t configureCameraDevice(camera2_device_t *dev,
+            MetadataQueue &requestQueue,
+            MetadataQueue  &frameQueue,
+            NotifierListener &listener) {
+
+        status_t err;
+
+        err = dev->ops->set_request_queue_src_ops(dev,
+                requestQueue.getToConsumerInterface());
+        if (err != OK) return err;
+
+        requestQueue.setFromConsumerInterface(dev);
+
+        err = dev->ops->set_frame_queue_dst_ops(dev,
+                frameQueue.getToProducerInterface());
+        if (err != OK) return err;
+
+        err = listener.getNotificationsFrom(dev);
+        if (err != OK) return err;
+
+        vendor_tag_query_ops_t *vendor_metadata_tag_ops;
+        err = dev->ops->get_metadata_vendor_tag_ops(dev, &vendor_metadata_tag_ops);
+        if (err != OK) return err;
+
+        err = set_camera_metadata_vendor_tag_ops(vendor_metadata_tag_ops);
+        if (err != OK) return err;
+
+        return OK;
+    }
+
+    static status_t closeCameraDevice(camera2_device_t *cam_dev) {
+        int res;
+        ALOGV("Closing camera %p", cam_dev);
+
+        hw_device_t *dev = reinterpret_cast<hw_device_t *>(cam_dev);
+        res = dev->close(dev);
+        return res;
+    }
+
+    void setUpCamera(int id) {
+        ASSERT_GT(sNumCameras, id);
+        status_t res;
+
+        if (mDevice != NULL) {
+            closeCameraDevice(mDevice);
+        }
+        mDevice = openCameraDevice(id);
+        ASSERT_TRUE(NULL != mDevice) << "Failed to open camera device";
+
+        camera_info info;
+        res = sCameraModule->get_camera_info(id, &info);
+        ASSERT_EQ(OK, res);
+
+        mStaticInfo = info.static_camera_characteristics;
+
+        res = configureCameraDevice(mDevice,
+                mRequests,
+                mFrames,
+                mNotifications);
+        ASSERT_EQ(OK, res) << "Failure to configure camera device";
+
+    }
+
+    void setUpStream(sp<ISurfaceTexture> consumer,
+            int width, int height, int format, int *id) {
+        status_t res;
+
+        StreamAdapter* stream = new StreamAdapter(consumer);
+
+        ALOGV("Creating stream, format 0x%x, %d x %d", format, width, height);
+        res = stream->connectToDevice(mDevice, width, height, format);
+        ASSERT_EQ(NO_ERROR, res) << "Failed to connect to stream: "
+                                 << strerror(-res);
+        mStreams.push_back(stream);
+
+        *id = stream->getId();
+    }
+
+    void disconnectStream(int id) {
+        status_t res;
+        unsigned int i=0;
+        for (; i < mStreams.size(); i++) {
+            if (mStreams[i]->getId() == id) {
+                res = mStreams[i]->disconnect();
+                ASSERT_EQ(NO_ERROR, res) <<
+                        "Failed to disconnect stream " << id;
+                break;
+            }
+        }
+        ASSERT_GT(mStreams.size(), i) << "Stream id not found:" << id;
+    }
+
+    void getResolutionList(int32_t format,
+            int32_t **list,
+            size_t *count) {
+        ALOGV("Getting resolutions for format %x", format);
+        status_t res;
+        if (format != CAMERA2_HAL_PIXEL_FORMAT_OPAQUE) {
+            camera_metadata_entry_t availableFormats;
+            res = find_camera_metadata_entry(mStaticInfo,
+                    ANDROID_SCALER_AVAILABLE_FORMATS,
+                    &availableFormats);
+            ASSERT_EQ(OK, res);
+
+            uint32_t formatIdx;
+            for (formatIdx=0; formatIdx < availableFormats.count; formatIdx++) {
+                if (availableFormats.data.i32[formatIdx] == format) break;
+            }
+            ASSERT_NE(availableFormats.count, formatIdx)
+                << "No support found for format 0x" << std::hex << format;
+        }
+
+        camera_metadata_entry_t availableSizes;
+        if (format == HAL_PIXEL_FORMAT_RAW_SENSOR) {
+            res = find_camera_metadata_entry(mStaticInfo,
+                    ANDROID_SCALER_AVAILABLE_RAW_SIZES,
+                    &availableSizes);
+        } else if (format == HAL_PIXEL_FORMAT_BLOB) {
+            res = find_camera_metadata_entry(mStaticInfo,
+                    ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
+                    &availableSizes);
+        } else {
+            res = find_camera_metadata_entry(mStaticInfo,
+                    ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
+                    &availableSizes);
+        }
+        ASSERT_EQ(OK, res);
+
+        *list = availableSizes.data.i32;
+        *count = availableSizes.count;
+    }
+
+    virtual void SetUp() {
+        const ::testing::TestInfo* const testInfo =
+                ::testing::UnitTest::GetInstance()->current_test_info();
+
+        ALOGV("*** Starting test %s in test case %s", testInfo->name(), testInfo->test_case_name());
+        mDevice = NULL;
+    }
+
+    virtual void TearDown() {
+        for (unsigned int i = 0; i < mStreams.size(); i++) {
+            delete mStreams[i];
+        }
+        if (mDevice != NULL) {
+            closeCameraDevice(mDevice);
+        }
+    }
+
+    camera2_device    *mDevice;
+    camera_metadata_t *mStaticInfo;
+
+    MetadataQueue    mRequests;
+    MetadataQueue    mFrames;
+    NotifierListener mNotifications;
+
+    Vector<StreamAdapter*> mStreams;
+
+  private:
+    static camera_module_t *sCameraModule;
+    static int              sNumCameras;
+    static bool            *sCameraSupportsHal2;
+};
+
+camera_module_t *Camera2Test::sCameraModule = NULL;
+bool *Camera2Test::sCameraSupportsHal2      = NULL;
+int Camera2Test::sNumCameras                = 0;
+
+static const nsecs_t USEC = 1000;
+static const nsecs_t MSEC = 1000*USEC;
+static const nsecs_t SEC = 1000*MSEC;
+
+
+TEST_F(Camera2Test, OpenClose) {
+    status_t res;
+
+    for (int id = 0; id < getNumCameras(); id++) {
+        if (!isHal2Supported(id)) continue;
+
+        camera2_device_t *d = openCameraDevice(id);
+        ASSERT_TRUE(NULL != d) << "Failed to open camera device";
+
+        res = closeCameraDevice(d);
+        ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
+    }
+}
+
+TEST_F(Camera2Test, Capture1Raw) {
+    status_t res;
+
+    for (int id = 0; id < getNumCameras(); id++) {
+        if (!isHal2Supported(id)) continue;
+
+        ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
+
+        sp<CpuConsumer> rawConsumer = new CpuConsumer(1);
+        sp<FrameWaiter> rawWaiter = new FrameWaiter();
+        rawConsumer->setFrameAvailableListener(rawWaiter);
+
+        int32_t *rawResolutions;
+        size_t   rawResolutionsCount;
+
+        int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
+
+        getResolutionList(format,
+                &rawResolutions, &rawResolutionsCount);
+        ASSERT_LT((size_t)0, rawResolutionsCount);
+
+        // Pick first available raw resolution
+        int width = rawResolutions[0];
+        int height = rawResolutions[1];
+
+        int streamId;
+        ASSERT_NO_FATAL_FAILURE(
+            setUpStream(rawConsumer->getProducerInterface(),
+                    width, height, format, &streamId) );
+
+        camera_metadata_t *request;
+        request = allocate_camera_metadata(20, 2000);
+
+        uint8_t metadataMode = ANDROID_REQUEST_METADATA_FULL;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_METADATA_MODE,
+                (void**)&metadataMode, 1);
+        uint32_t outputStreams = streamId;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_OUTPUT_STREAMS,
+                (void**)&outputStreams, 1);
+
+        uint64_t exposureTime = 10*MSEC;
+        add_camera_metadata_entry(request,
+                ANDROID_SENSOR_EXPOSURE_TIME,
+                (void**)&exposureTime, 1);
+        uint64_t frameDuration = 30*MSEC;
+        add_camera_metadata_entry(request,
+                ANDROID_SENSOR_FRAME_DURATION,
+                (void**)&frameDuration, 1);
+        uint32_t sensitivity = 100;
+        add_camera_metadata_entry(request,
+                ANDROID_SENSOR_SENSITIVITY,
+                (void**)&sensitivity, 1);
+
+        uint32_t hourOfDay = 12;
+        add_camera_metadata_entry(request,
+                0x80000000, // EMULATOR_HOUROFDAY
+                &hourOfDay, 1);
+
+        IF_ALOGV() {
+            std::cout << "Input request: " << std::endl;
+            dump_camera_metadata(request, 0, 1);
+        }
+
+        res = mRequests.enqueue(request);
+        ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
+
+        res = mFrames.waitForBuffer(exposureTime + SEC);
+        ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
+
+        camera_metadata_t *frame;
+        res = mFrames.dequeue(&frame);
+        ASSERT_EQ(NO_ERROR, res);
+        ASSERT_TRUE(frame != NULL);
+
+        IF_ALOGV() {
+            std::cout << "Output frame:" << std::endl;
+            dump_camera_metadata(frame, 0, 1);
+        }
+
+        res = rawWaiter->waitForFrame(exposureTime + SEC);
+        ASSERT_EQ(NO_ERROR, res);
+
+        CpuConsumer::LockedBuffer buffer;
+        res = rawConsumer->lockNextBuffer(&buffer);
+        ASSERT_EQ(NO_ERROR, res);
+
+        IF_ALOGV() {
+            const char *dumpname =
+                    "/data/local/tmp/camera2_test-capture1raw-dump.raw";
+            ALOGV("Dumping raw buffer to %s", dumpname);
+            // Write to file
+            std::ofstream rawFile(dumpname);
+            size_t bpp = 2;
+            for (unsigned int y = 0; y < buffer.height; y++) {
+                rawFile.write(
+                        (const char *)(buffer.data + y * buffer.stride * bpp),
+                        buffer.width * bpp);
+            }
+            rawFile.close();
+        }
+
+        res = rawConsumer->unlockBuffer(buffer);
+        ASSERT_EQ(NO_ERROR, res);
+
+        ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
+
+        res = closeCameraDevice(mDevice);
+        ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
+
+    }
+}
+
+TEST_F(Camera2Test, CaptureBurstRaw) {
+    status_t res;
+
+    for (int id = 0; id < getNumCameras(); id++) {
+        if (!isHal2Supported(id)) continue;
+
+        ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
+
+        sp<CpuConsumer> rawConsumer = new CpuConsumer(1);
+        sp<FrameWaiter> rawWaiter = new FrameWaiter();
+        rawConsumer->setFrameAvailableListener(rawWaiter);
+
+        int32_t *rawResolutions;
+        size_t    rawResolutionsCount;
+
+        int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
+
+        getResolutionList(format,
+                &rawResolutions, &rawResolutionsCount);
+        ASSERT_LT((uint32_t)0, rawResolutionsCount);
+
+        // Pick first available raw resolution
+        int width = rawResolutions[0];
+        int height = rawResolutions[1];
+
+        int streamId;
+        ASSERT_NO_FATAL_FAILURE(
+            setUpStream(rawConsumer->getProducerInterface(),
+                    width, height, format, &streamId) );
+
+        camera_metadata_t *request;
+        request = allocate_camera_metadata(20, 2000);
+
+        uint8_t metadataMode = ANDROID_REQUEST_METADATA_FULL;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_METADATA_MODE,
+                (void**)&metadataMode, 1);
+        uint32_t outputStreams = streamId;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_OUTPUT_STREAMS,
+                (void**)&outputStreams, 1);
+
+        uint64_t frameDuration = 30*MSEC;
+        add_camera_metadata_entry(request,
+                ANDROID_SENSOR_FRAME_DURATION,
+                (void**)&frameDuration, 1);
+        uint32_t sensitivity = 100;
+        add_camera_metadata_entry(request,
+                ANDROID_SENSOR_SENSITIVITY,
+                (void**)&sensitivity, 1);
+
+        uint32_t hourOfDay = 12;
+        add_camera_metadata_entry(request,
+                0x80000000, // EMULATOR_HOUROFDAY
+                &hourOfDay, 1);
+
+        IF_ALOGV() {
+            std::cout << "Input request template: " << std::endl;
+            dump_camera_metadata(request, 0, 1);
+        }
+
+        int numCaptures = 10;
+
+        // Enqueue numCaptures requests with increasing exposure time
+
+        uint64_t exposureTime = 100 * USEC;
+        for (int reqCount = 0; reqCount < numCaptures; reqCount++ ) {
+            camera_metadata_t *req;
+            req = allocate_camera_metadata(20, 2000);
+            append_camera_metadata(req, request);
+
+            add_camera_metadata_entry(req,
+                    ANDROID_SENSOR_EXPOSURE_TIME,
+                    (void**)&exposureTime, 1);
+            exposureTime *= 2;
+
+            res = mRequests.enqueue(req);
+            ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: "
+                    << strerror(-res);
+        }
+
+        // Get frames and image buffers one by one
+        uint64_t expectedExposureTime = 100 * USEC;
+        for (int frameCount = 0; frameCount < 10; frameCount++) {
+            res = mFrames.waitForBuffer(SEC + expectedExposureTime);
+            ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
+
+            camera_metadata_t *frame;
+            res = mFrames.dequeue(&frame);
+            ASSERT_EQ(NO_ERROR, res);
+            ASSERT_TRUE(frame != NULL);
+
+            camera_metadata_entry_t frameNumber;
+            res = find_camera_metadata_entry(frame,
+                    ANDROID_REQUEST_FRAME_COUNT,
+                    &frameNumber);
+            ASSERT_EQ(NO_ERROR, res);
+            ASSERT_EQ(frameCount, *frameNumber.data.i32);
+
+            res = rawWaiter->waitForFrame(SEC + expectedExposureTime);
+            ASSERT_EQ(NO_ERROR, res) <<
+                    "Never got raw data for capture " << frameCount;
+
+            CpuConsumer::LockedBuffer buffer;
+            res = rawConsumer->lockNextBuffer(&buffer);
+            ASSERT_EQ(NO_ERROR, res);
+
+            IF_ALOGV() {
+                char dumpname[60];
+                snprintf(dumpname, 60,
+                        "/data/local/tmp/camera2_test-"
+                        "captureBurstRaw-dump_%d.raw",
+                        frameCount);
+                ALOGV("Dumping raw buffer to %s", dumpname);
+                // Write to file
+                std::ofstream rawFile(dumpname);
+                for (unsigned int y = 0; y < buffer.height; y++) {
+                    rawFile.write(
+                            (const char *)(buffer.data + y * buffer.stride * 2),
+                            buffer.width * 2);
+                }
+                rawFile.close();
+            }
+
+            res = rawConsumer->unlockBuffer(buffer);
+            ASSERT_EQ(NO_ERROR, res);
+
+            expectedExposureTime *= 2;
+        }
+    }
+}
+
+TEST_F(Camera2Test, ConstructDefaultRequests) {
+    status_t res;
+
+    for (int id = 0; id < getNumCameras(); id++) {
+        if (!isHal2Supported(id)) continue;
+
+        ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
+
+        for (int i = CAMERA2_TEMPLATE_PREVIEW; i < CAMERA2_TEMPLATE_COUNT;
+             i++) {
+            camera_metadata_t *request = NULL;
+            res = mDevice->ops->construct_default_request(mDevice,
+                    i,
+                    &request);
+            EXPECT_EQ(NO_ERROR, res) <<
+                    "Unable to construct request from template type %d", i;
+            EXPECT_TRUE(request != NULL);
+            EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request));
+            EXPECT_LT((size_t)0, get_camera_metadata_data_count(request));
+
+            IF_ALOGV() {
+                std::cout << "  ** Template type " << i << ":"<<std::endl;
+                dump_camera_metadata(request, 0, 2);
+            }
+
+            free_camera_metadata(request);
+        }
+    }
+}
+
+TEST_F(Camera2Test, Capture1Jpeg) {
+    status_t res;
+
+    for (int id = 0; id < getNumCameras(); id++) {
+        if (!isHal2Supported(id)) continue;
+
+        ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
+
+        sp<CpuConsumer> jpegConsumer = new CpuConsumer(1);
+        sp<FrameWaiter> jpegWaiter = new FrameWaiter();
+        jpegConsumer->setFrameAvailableListener(jpegWaiter);
+
+        int32_t *jpegResolutions;
+        size_t   jpegResolutionsCount;
+
+        int format = HAL_PIXEL_FORMAT_BLOB;
+
+        getResolutionList(format,
+                &jpegResolutions, &jpegResolutionsCount);
+        ASSERT_LT((size_t)0, jpegResolutionsCount);
+
+        // Pick first available JPEG resolution
+        int width = jpegResolutions[0];
+        int height = jpegResolutions[1];
+
+        int streamId;
+        ASSERT_NO_FATAL_FAILURE(
+            setUpStream(jpegConsumer->getProducerInterface(),
+                    width, height, format, &streamId) );
+
+        camera_metadata_t *request;
+        request = allocate_camera_metadata(20, 2000);
+
+        uint8_t metadataMode = ANDROID_REQUEST_METADATA_FULL;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_METADATA_MODE,
+                (void**)&metadataMode, 1);
+        uint32_t outputStreams = streamId;
+        add_camera_metadata_entry(request,
+                ANDROID_REQUEST_OUTPUT_STREAMS,
+                (void**)&outputStreams, 1);
+
+        uint64_t exposureTime = 10*MSEC;
+        add_camera_metadata_entry(request,
+                ANDROID_SENSOR_EXPOSURE_TIME,
+                (void**)&exposureTime, 1);
+        uint64_t frameDuration = 30*MSEC;
+        add_camera_metadata_entry(request,
+                ANDROID_SENSOR_FRAME_DURATION,
+                (void**)&frameDuration, 1);
+        uint32_t sensitivity = 100;
+        add_camera_metadata_entry(request,
+                ANDROID_SENSOR_SENSITIVITY,
+                (void**)&sensitivity, 1);
+
+        uint32_t hourOfDay = 12;
+        add_camera_metadata_entry(request,
+                0x80000000, // EMULATOR_HOUROFDAY
+                &hourOfDay, 1);
+
+        IF_ALOGV() {
+            std::cout << "Input request: " << std::endl;
+            dump_camera_metadata(request, 0, 1);
+        }
+
+        res = mRequests.enqueue(request);
+        ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
+
+        res = mFrames.waitForBuffer(exposureTime + SEC);
+        ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
+
+        camera_metadata_t *frame;
+        res = mFrames.dequeue(&frame);
+        ASSERT_EQ(NO_ERROR, res);
+        ASSERT_TRUE(frame != NULL);
+
+        IF_ALOGV() {
+            std::cout << "Output frame:" << std::endl;
+            dump_camera_metadata(frame, 0, 1);
+        }
+
+        res = jpegWaiter->waitForFrame(exposureTime + SEC);
+        ASSERT_EQ(NO_ERROR, res);
+
+        CpuConsumer::LockedBuffer buffer;
+        res = jpegConsumer->lockNextBuffer(&buffer);
+        ASSERT_EQ(NO_ERROR, res);
+
+        IF_ALOGV() {
+            const char *dumpname =
+                    "/data/local/tmp/camera2_test-capture1jpeg-dump.jpeg";
+            ALOGV("Dumping raw buffer to %s", dumpname);
+            // Write to file
+            std::ofstream jpegFile(dumpname);
+            size_t bpp = 1;
+            for (unsigned int y = 0; y < buffer.height; y++) {
+                jpegFile.write(
+                        (const char *)(buffer.data + y * buffer.stride * bpp),
+                        buffer.width * bpp);
+            }
+            jpegFile.close();
+        }
+
+        res = jpegConsumer->unlockBuffer(buffer);
+        ASSERT_EQ(NO_ERROR, res);
+
+        ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
+
+        res = closeCameraDevice(mDevice);
+        ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
+
+    }
+}
+
+
+} // namespace android
diff --git a/tests/camera2/camera2_utils.cpp b/tests/camera2/camera2_utils.cpp
new file mode 100644
index 0000000..ba938d9
--- /dev/null
+++ b/tests/camera2/camera2_utils.cpp
@@ -0,0 +1,582 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Utility classes for camera2 HAL testing
+
+#define LOG_TAG "Camera2_test_utils"
+#define LOG_NDEBUG 0
+
+#include "utils/Log.h"
+#include "camera2_utils.h"
+
+namespace android {
+
+/**
+ * MetadataQueue
+ */
+
+MetadataQueue::MetadataQueue():
+            mDevice(NULL),
+            mFrameCount(0),
+            mCount(0),
+            mStreamSlotCount(0),
+            mSignalConsumer(true)
+{
+    camera2_request_queue_src_ops::dequeue_request = consumer_dequeue;
+    camera2_request_queue_src_ops::request_count = consumer_buffer_count;
+    camera2_request_queue_src_ops::free_request = consumer_free;
+
+    camera2_frame_queue_dst_ops::dequeue_frame = producer_dequeue;
+    camera2_frame_queue_dst_ops::cancel_frame = producer_cancel;
+    camera2_frame_queue_dst_ops::enqueue_frame = producer_enqueue;
+}
+
+MetadataQueue::~MetadataQueue() {
+    freeBuffers(mEntries.begin(), mEntries.end());
+    freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
+}
+
+// Interface to camera2 HAL as consumer (input requests/reprocessing)
+const camera2_request_queue_src_ops_t* MetadataQueue::getToConsumerInterface() {
+    return static_cast<camera2_request_queue_src_ops_t*>(this);
+}
+
+void MetadataQueue::setFromConsumerInterface(camera2_device_t *d) {
+    mDevice = d;
+}
+
+const camera2_frame_queue_dst_ops_t* MetadataQueue::getToProducerInterface() {
+    return static_cast<camera2_frame_queue_dst_ops_t*>(this);
+}
+
+// Real interfaces
+status_t MetadataQueue::enqueue(camera_metadata_t *buf) {
+    Mutex::Autolock l(mMutex);
+
+    mCount++;
+    mEntries.push_back(buf);
+    notEmpty.signal();
+
+    if (mSignalConsumer && mDevice != NULL) {
+        mSignalConsumer = false;
+
+        mMutex.unlock();
+        ALOGV("%s: Signaling consumer", __FUNCTION__);
+        mDevice->ops->notify_request_queue_not_empty(mDevice);
+        mMutex.lock();
+    }
+    return OK;
+}
+
+int MetadataQueue::getBufferCount() {
+    Mutex::Autolock l(mMutex);
+    if (mStreamSlotCount > 0) {
+        return CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS;
+    }
+    return mCount;
+}
+
+status_t MetadataQueue::dequeue(camera_metadata_t **buf, bool incrementCount) {
+    Mutex::Autolock l(mMutex);
+
+    if (mCount == 0) {
+        if (mStreamSlotCount == 0) {
+            ALOGV("%s: Empty", __FUNCTION__);
+            *buf = NULL;
+            mSignalConsumer = true;
+            return OK;
+        }
+        ALOGV("%s: Streaming %d frames to queue", __FUNCTION__,
+              mStreamSlotCount);
+
+        for (List<camera_metadata_t*>::iterator slotEntry = mStreamSlot.begin();
+                slotEntry != mStreamSlot.end();
+                slotEntry++ ) {
+            size_t entries = get_camera_metadata_entry_count(*slotEntry);
+            size_t dataBytes = get_camera_metadata_data_count(*slotEntry);
+
+            camera_metadata_t *copy = allocate_camera_metadata(entries, dataBytes);
+            append_camera_metadata(copy, *slotEntry);
+            mEntries.push_back(copy);
+        }
+        mCount = mStreamSlotCount;
+    }
+    ALOGV("MetadataQueue: deque (%d buffers)", mCount);
+    camera_metadata_t *b = *(mEntries.begin());
+    mEntries.erase(mEntries.begin());
+
+    if (incrementCount) {
+        add_camera_metadata_entry(b,
+                ANDROID_REQUEST_FRAME_COUNT,
+                (void**)&mFrameCount, 1);
+        mFrameCount++;
+    }
+
+    *buf = b;
+    mCount--;
+
+    return OK;
+}
+
+status_t MetadataQueue::waitForBuffer(nsecs_t timeout) {
+    Mutex::Autolock l(mMutex);
+    status_t res;
+    while (mCount == 0) {
+        res = notEmpty.waitRelative(mMutex,timeout);
+        if (res != OK) return res;
+    }
+    return OK;
+}
+
+status_t MetadataQueue::setStreamSlot(camera_metadata_t *buf) {
+    if (buf == NULL) {
+        freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
+        mStreamSlotCount = 0;
+        return OK;
+    }
+    if (mStreamSlotCount > 1) {
+        List<camera_metadata_t*>::iterator deleter = ++mStreamSlot.begin();
+        freeBuffers(++mStreamSlot.begin(), mStreamSlot.end());
+        mStreamSlotCount = 1;
+    }
+    if (mStreamSlotCount == 1) {
+        free_camera_metadata( *(mStreamSlot.begin()) );
+        *(mStreamSlot.begin()) = buf;
+    } else {
+        mStreamSlot.push_front(buf);
+        mStreamSlotCount = 1;
+    }
+    return OK;
+}
+
+status_t MetadataQueue::setStreamSlot(const List<camera_metadata_t*> &bufs) {
+    if (mStreamSlotCount > 0) {
+        freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
+    }
+    mStreamSlot = bufs;
+    mStreamSlotCount = mStreamSlot.size();
+
+    return OK;
+}
+
+status_t MetadataQueue::freeBuffers(List<camera_metadata_t*>::iterator start,
+                                    List<camera_metadata_t*>::iterator end) {
+    while (start != end) {
+        free_camera_metadata(*start);
+        start = mStreamSlot.erase(start);
+    }
+    return OK;
+}
+
+MetadataQueue* MetadataQueue::getInstance(
+        const camera2_request_queue_src_ops_t *q) {
+    const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
+    return const_cast<MetadataQueue*>(cmq);
+}
+
+MetadataQueue* MetadataQueue::getInstance(
+        const camera2_frame_queue_dst_ops_t *q) {
+    const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
+    return const_cast<MetadataQueue*>(cmq);
+}
+
+int MetadataQueue::consumer_buffer_count(
+        const camera2_request_queue_src_ops_t *q) {
+    MetadataQueue *queue = getInstance(q);
+    return queue->getBufferCount();
+}
+
+int MetadataQueue::consumer_dequeue(const camera2_request_queue_src_ops_t *q,
+        camera_metadata_t **buffer) {
+    MetadataQueue *queue = getInstance(q);
+    return queue->dequeue(buffer, true);
+}
+
+int MetadataQueue::consumer_free(const camera2_request_queue_src_ops_t *q,
+        camera_metadata_t *old_buffer) {
+    MetadataQueue *queue = getInstance(q);
+    free_camera_metadata(old_buffer);
+    return OK;
+}
+
+int MetadataQueue::producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
+        size_t entries, size_t bytes,
+        camera_metadata_t **buffer) {
+    camera_metadata_t *new_buffer =
+            allocate_camera_metadata(entries, bytes);
+    if (new_buffer == NULL) return NO_MEMORY;
+    *buffer = new_buffer;
+        return OK;
+}
+
+int MetadataQueue::producer_cancel(const camera2_frame_queue_dst_ops_t *q,
+        camera_metadata_t *old_buffer) {
+    free_camera_metadata(old_buffer);
+    return OK;
+}
+
+int MetadataQueue::producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
+        camera_metadata_t *filled_buffer) {
+    MetadataQueue *queue = getInstance(q);
+    return queue->enqueue(filled_buffer);
+}
+
+/**
+ * NotifierListener
+ */
+
+NotifierListener::NotifierListener() {
+}
+
+status_t NotifierListener::getNotificationsFrom(camera2_device *dev) {
+    if (!dev) return BAD_VALUE;
+    status_t err;
+    err = dev->ops->set_notify_callback(dev,
+            notify_callback_dispatch,
+            (void*)this);
+    return err;
+}
+
+status_t NotifierListener::getNextNotification(int32_t *msg_type,
+        int32_t *ext1,
+        int32_t *ext2,
+        int32_t *ext3) {
+    Mutex::Autolock l(mMutex);
+    if (mNotifications.size() == 0) return BAD_VALUE;
+    return getNextNotificationLocked(msg_type, ext1, ext2, ext3);
+}
+
+status_t NotifierListener::waitForNotification(int32_t *msg_type,
+        int32_t *ext1,
+        int32_t *ext2,
+        int32_t *ext3) {
+    Mutex::Autolock l(mMutex);
+    while (mNotifications.size() == 0) {
+        mNewNotification.wait(mMutex);
+    }
+    return getNextNotificationLocked(msg_type, ext1, ext2, ext3);
+}
+
+int NotifierListener::numNotifications() {
+    Mutex::Autolock l(mMutex);
+    return mNotifications.size();
+}
+
+status_t NotifierListener::getNextNotificationLocked(int32_t *msg_type,
+        int32_t *ext1,
+        int32_t *ext2,
+        int32_t *ext3) {
+    *msg_type = mNotifications.begin()->msg_type;
+    *ext1 = mNotifications.begin()->ext1;
+    *ext2 = mNotifications.begin()->ext2;
+    *ext3 = mNotifications.begin()->ext3;
+    mNotifications.erase(mNotifications.begin());
+    return OK;
+}
+
+void NotifierListener::onNotify(int32_t msg_type,
+        int32_t ext1,
+        int32_t ext2,
+        int32_t ext3) {
+    Mutex::Autolock l(mMutex);
+    mNotifications.push_back(Notification(msg_type, ext1, ext2, ext3));
+    mNewNotification.signal();
+}
+
+void NotifierListener::notify_callback_dispatch(int32_t msg_type,
+        int32_t ext1,
+        int32_t ext2,
+        int32_t ext3,
+        void *user) {
+    NotifierListener *me = reinterpret_cast<NotifierListener*>(user);
+    me->onNotify(msg_type, ext1, ext2, ext3);
+}
+
+/**
+ * StreamAdapter
+ */
+
+#ifndef container_of
+#define container_of(ptr, type, member) \
+    (type *)((char*)(ptr) - offsetof(type, member))
+#endif
+
+StreamAdapter::StreamAdapter(sp<ISurfaceTexture> consumer):
+        mState(UNINITIALIZED), mDevice(NULL),
+        mId(-1),
+        mWidth(0), mHeight(0), mFormatRequested(0)
+{
+    mConsumerInterface = new SurfaceTextureClient(consumer);
+    camera2_stream_ops::dequeue_buffer = dequeue_buffer;
+    camera2_stream_ops::enqueue_buffer = enqueue_buffer;
+    camera2_stream_ops::cancel_buffer = cancel_buffer;
+    camera2_stream_ops::set_crop = set_crop;
+}
+
+StreamAdapter::~StreamAdapter() {
+    disconnect();
+}
+
+status_t StreamAdapter::connectToDevice(camera2_device_t *d,
+        uint32_t width, uint32_t height, int format) {
+    if (mState != UNINITIALIZED) return INVALID_OPERATION;
+    if (d == NULL) {
+        ALOGE("%s: Null device passed to stream adapter", __FUNCTION__);
+        return BAD_VALUE;
+    }
+
+    status_t res;
+
+    mWidth = width;
+    mHeight = height;
+    mFormatRequested = format;
+
+    // Allocate device-side stream interface
+
+    uint32_t id;
+    uint32_t formatActual;
+    uint32_t usage;
+    uint32_t maxBuffers = 2;
+    res = d->ops->allocate_stream(d,
+            mWidth, mHeight, mFormatRequested, getStreamOps(),
+            &id, &formatActual, &usage, &maxBuffers);
+    if (res != OK) {
+        ALOGE("%s: Device stream allocation failed: %s (%d)",
+                __FUNCTION__, strerror(-res), res);
+        mState = UNINITIALIZED;
+        return res;
+    }
+    mDevice = d;
+
+    mId = id;
+    mFormat = formatActual;
+    mUsage = usage;
+    mMaxProducerBuffers = maxBuffers;
+
+    // Configure consumer-side ANativeWindow interface
+
+    res = native_window_api_connect(mConsumerInterface.get(),
+            NATIVE_WINDOW_API_CAMERA);
+    if (res != OK) {
+        ALOGE("%s: Unable to connect to native window for stream %d",
+                __FUNCTION__, mId);
+        mState = ALLOCATED;
+        return res;
+    }
+
+    res = native_window_set_usage(mConsumerInterface.get(), mUsage);
+    if (res != OK) {
+        ALOGE("%s: Unable to configure usage %08x for stream %d",
+                __FUNCTION__, mUsage, mId);
+        mState = CONNECTED;
+        return res;
+    }
+
+    res = native_window_set_buffers_geometry(mConsumerInterface.get(),
+            mWidth, mHeight, mFormat);
+    if (res != OK) {
+        ALOGE("%s: Unable to configure buffer geometry"
+                " %d x %d, format 0x%x for stream %d",
+                __FUNCTION__, mWidth, mHeight, mFormat, mId);
+        mState = CONNECTED;
+        return res;
+    }
+
+    int maxConsumerBuffers;
+    res = mConsumerInterface->query(mConsumerInterface.get(),
+            NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
+    if (res != OK) {
+        ALOGE("%s: Unable to query consumer undequeued"
+                " buffer count for stream %d", __FUNCTION__, mId);
+        mState = CONNECTED;
+        return res;
+    }
+    mMaxConsumerBuffers = maxConsumerBuffers;
+
+    ALOGV("%s: Producer wants %d buffers, consumer wants %d", __FUNCTION__,
+            mMaxProducerBuffers, mMaxConsumerBuffers);
+
+    int totalBuffers = mMaxConsumerBuffers + mMaxProducerBuffers;
+
+    res = native_window_set_buffer_count(mConsumerInterface.get(),
+            totalBuffers);
+    if (res != OK) {
+        ALOGE("%s: Unable to set buffer count for stream %d",
+                __FUNCTION__, mId);
+        mState = CONNECTED;
+        return res;
+    }
+
+    // Register allocated buffers with HAL device
+    buffer_handle_t *buffers = new buffer_handle_t[totalBuffers];
+    ANativeWindowBuffer **anwBuffers = new ANativeWindowBuffer*[totalBuffers];
+    int bufferIdx = 0;
+    for (; bufferIdx < totalBuffers; bufferIdx++) {
+        res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
+                &anwBuffers[bufferIdx]);
+        if (res != OK) {
+            ALOGE("%s: Unable to dequeue buffer %d for initial registration for"
+                    "stream %d", __FUNCTION__, bufferIdx, mId);
+            mState = CONNECTED;
+            goto cleanUpBuffers;
+        }
+        buffers[bufferIdx] = anwBuffers[bufferIdx]->handle;
+    }
+
+    res = mDevice->ops->register_stream_buffers(mDevice,
+            mId,
+            totalBuffers,
+            buffers);
+    if (res != OK) {
+        ALOGE("%s: Unable to register buffers with HAL device for stream %d",
+                __FUNCTION__, mId);
+        mState = CONNECTED;
+    } else {
+        mState = ACTIVE;
+    }
+
+cleanUpBuffers:
+    for (int i = 0; i < bufferIdx; i++) {
+        res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
+                anwBuffers[i], -1);
+    }
+    delete anwBuffers;
+    delete buffers;
+
+    return res;
+}
+
+status_t StreamAdapter::disconnect() {
+    status_t res;
+    if (mState >= ALLOCATED) {
+        res = mDevice->ops->release_stream(mDevice, mId);
+        if (res != OK) {
+            ALOGE("%s: Unable to release stream %d",
+                    __FUNCTION__, mId);
+            return res;
+        }
+    }
+    if (mState >= CONNECTED) {
+        res = native_window_api_disconnect(mConsumerInterface.get(),
+                NATIVE_WINDOW_API_CAMERA);
+        if (res != OK) {
+            ALOGE("%s: Unable to disconnect stream %d from native window",
+                    __FUNCTION__, mId);
+            return res;
+        }
+    }
+    mId = -1;
+    mState = DISCONNECTED;
+    return OK;
+}
+
+int StreamAdapter::getId() {
+    return mId;
+}
+
+const camera2_stream_ops *StreamAdapter::getStreamOps() {
+    return static_cast<camera2_stream_ops *>(this);
+}
+
+ANativeWindow* StreamAdapter::toANW(const camera2_stream_ops_t *w) {
+    return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
+}
+
+int StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
+        buffer_handle_t** buffer) {
+    int res;
+    int state = static_cast<const StreamAdapter*>(w)->mState;
+    if (state != ACTIVE) {
+        ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
+        return INVALID_OPERATION;
+    }
+
+    ANativeWindow *a = toANW(w);
+    ANativeWindowBuffer* anb;
+    res = native_window_dequeue_buffer_and_wait(a, &anb);
+    if (res != OK) return res;
+
+    *buffer = &(anb->handle);
+
+    return res;
+}
+
+int StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
+        int64_t timestamp,
+        buffer_handle_t* buffer) {
+    int state = static_cast<const StreamAdapter*>(w)->mState;
+    if (state != ACTIVE) {
+        ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
+        return INVALID_OPERATION;
+    }
+    ANativeWindow *a = toANW(w);
+    status_t err;
+    err = native_window_set_buffers_timestamp(a, timestamp);
+    if (err != OK) return err;
+    return a->queueBuffer(a,
+            container_of(buffer, ANativeWindowBuffer, handle), -1);
+}
+
+int StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
+        buffer_handle_t* buffer) {
+    int state = static_cast<const StreamAdapter*>(w)->mState;
+    if (state != ACTIVE) {
+        ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
+        return INVALID_OPERATION;
+    }
+    ANativeWindow *a = toANW(w);
+    return a->cancelBuffer(a,
+            container_of(buffer, ANativeWindowBuffer, handle), -1);
+}
+
+int StreamAdapter::set_crop(const camera2_stream_ops_t* w,
+        int left, int top, int right, int bottom) {
+    int state = static_cast<const StreamAdapter*>(w)->mState;
+    if (state != ACTIVE) {
+        ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
+        return INVALID_OPERATION;
+    }
+    ANativeWindow *a = toANW(w);
+    android_native_rect_t crop = { left, top, right, bottom };
+    return native_window_set_crop(a, &crop);
+}
+
+/**
+ * FrameWaiter
+ */
+
+FrameWaiter::FrameWaiter():
+        mPendingFrames(0) {
+}
+
+status_t FrameWaiter::waitForFrame(nsecs_t timeout) {
+    status_t res;
+    Mutex::Autolock lock(mMutex);
+    while (mPendingFrames == 0) {
+        res = mCondition.waitRelative(mMutex, timeout);
+        if (res != OK) return res;
+    }
+    mPendingFrames--;
+    return OK;
+}
+
+void FrameWaiter::onFrameAvailable() {
+    Mutex::Autolock lock(mMutex);
+    mPendingFrames++;
+    mCondition.signal();
+}
+
+} // namespace android
diff --git a/tests/camera2/camera2_utils.h b/tests/camera2/camera2_utils.h
new file mode 100644
index 0000000..2c9f801
--- /dev/null
+++ b/tests/camera2/camera2_utils.h
@@ -0,0 +1,238 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+// Utility classes for camera2 HAL testing
+
+#include <system/camera_metadata.h>
+#include <hardware/camera2.h>
+
+#include <gui/SurfaceTextureClient.h>
+#include <gui/CpuConsumer.h>
+
+#include <utils/List.h>
+#include <utils/Mutex.h>
+#include <utils/Condition.h>
+
+namespace android {
+
+/**
+ * Queue class for both sending requests to a camera2 device, and for receiving
+ * frames from a camera2 device.
+ */
+class MetadataQueue: public camera2_request_queue_src_ops_t,
+                    public camera2_frame_queue_dst_ops_t {
+  public:
+    MetadataQueue();
+    ~MetadataQueue();
+
+    // Interface to camera2 HAL device, either for requests (device is consumer)
+    // or for frames (device is producer)
+    const camera2_request_queue_src_ops_t*   getToConsumerInterface();
+    void setFromConsumerInterface(camera2_device_t *d);
+
+    const camera2_frame_queue_dst_ops_t* getToProducerInterface();
+
+    // Real interfaces. On enqueue, queue takes ownership of buffer pointer
+    // On dequeue, user takes ownership of buffer pointer.
+    status_t enqueue(camera_metadata_t *buf);
+    status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
+    int      getBufferCount();
+    status_t waitForBuffer(nsecs_t timeout);
+
+    // Set repeating buffer(s); if the queue is empty on a dequeue call, the
+    // queue copies the contents of the stream slot into the queue, and then
+    // dequeues the first new entry.
+    status_t setStreamSlot(camera_metadata_t *buf);
+    status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
+
+  private:
+    status_t freeBuffers(List<camera_metadata_t*>::iterator start,
+                         List<camera_metadata_t*>::iterator end);
+
+    camera2_device_t *mDevice;
+
+    Mutex mMutex;
+    Condition notEmpty;
+
+    int mFrameCount;
+
+    int mCount;
+    List<camera_metadata_t*> mEntries;
+    int mStreamSlotCount;
+    List<camera_metadata_t*> mStreamSlot;
+
+    bool mSignalConsumer;
+
+    static MetadataQueue* getInstance(const camera2_frame_queue_dst_ops_t *q);
+    static MetadataQueue* getInstance(const camera2_request_queue_src_ops_t *q);
+
+    static int consumer_buffer_count(const camera2_request_queue_src_ops_t *q);
+
+    static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
+            camera_metadata_t **buffer);
+
+    static int consumer_free(const camera2_request_queue_src_ops_t *q,
+            camera_metadata_t *old_buffer);
+
+    static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
+            size_t entries, size_t bytes,
+            camera_metadata_t **buffer);
+
+    static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
+            camera_metadata_t *old_buffer);
+
+    static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
+            camera_metadata_t *filled_buffer);
+
+};
+
+/**
+ * Basic class to receive and queue up notifications from the camera device
+ */
+
+class NotifierListener {
+  public:
+
+    NotifierListener();
+
+    status_t getNotificationsFrom(camera2_device *dev);
+
+    status_t getNextNotification(int32_t *msg_type, int32_t *ext1,
+            int32_t *ext2, int32_t *ext3);
+
+    status_t waitForNotification(int32_t *msg_type, int32_t *ext1,
+            int32_t *ext2, int32_t *ext3);
+
+    int numNotifications();
+
+  private:
+
+    status_t getNextNotificationLocked(int32_t *msg_type,
+            int32_t *ext1, int32_t *ext2, int32_t *ext3);
+
+    struct Notification {
+        Notification(int32_t type, int32_t e1, int32_t e2, int32_t e3):
+                msg_type(type),
+                ext1(e1),
+                ext2(e2),
+                ext3(e3)
+        {}
+
+        int32_t msg_type;
+        int32_t ext1;
+        int32_t ext2;
+        int32_t ext3;
+    };
+
+    List<Notification> mNotifications;
+
+    Mutex mMutex;
+    Condition mNewNotification;
+
+    void onNotify(int32_t msg_type,
+            int32_t ext1,
+            int32_t ext2,
+            int32_t ext3);
+
+    static void notify_callback_dispatch(int32_t msg_type,
+            int32_t ext1,
+            int32_t ext2,
+            int32_t ext3,
+            void *user);
+
+};
+
+/**
+ * Adapter from an ISurfaceTexture interface to camera2 device stream ops.
+ * Also takes care of allocating/deallocating stream in device interface
+ */
+class StreamAdapter: public camera2_stream_ops {
+  public:
+    StreamAdapter(sp<ISurfaceTexture> consumer);
+
+    ~StreamAdapter();
+
+    status_t connectToDevice(camera2_device_t *d,
+            uint32_t width, uint32_t height, int format);
+
+    status_t disconnect();
+
+    // Get stream ID. Only valid after a successful connectToDevice call.
+    int      getId();
+
+  private:
+    enum {
+        ERROR = -1,
+        DISCONNECTED = 0,
+        UNINITIALIZED,
+        ALLOCATED,
+        CONNECTED,
+        ACTIVE
+    } mState;
+
+    sp<ANativeWindow> mConsumerInterface;
+    camera2_device_t *mDevice;
+
+    uint32_t mId;
+    uint32_t mWidth;
+    uint32_t mHeight;
+    uint32_t mFormat;
+    uint32_t mUsage;
+    uint32_t mMaxProducerBuffers;
+    uint32_t mMaxConsumerBuffers;
+
+    int mFormatRequested;
+
+    const camera2_stream_ops *getStreamOps();
+
+    static ANativeWindow* toANW(const camera2_stream_ops_t *w);
+
+    static int dequeue_buffer(const camera2_stream_ops_t *w,
+            buffer_handle_t** buffer);
+
+    static int enqueue_buffer(const camera2_stream_ops_t* w,
+            int64_t timestamp,
+            buffer_handle_t* buffer);
+
+    static int cancel_buffer(const camera2_stream_ops_t* w,
+            buffer_handle_t* buffer);
+
+    static int set_crop(const camera2_stream_ops_t* w,
+            int left, int top, int right, int bottom);
+
+};
+
+/**
+ * Simple class to wait on the CpuConsumer to have a frame available
+ */
+class FrameWaiter : public CpuConsumer::FrameAvailableListener {
+  public:
+    FrameWaiter();
+
+    /**
+     * Wait for max timeout nanoseconds for a new frame. Returns
+     * OK if a frame is available, TIMED_OUT if the timeout was reached.
+     */
+    status_t waitForFrame(nsecs_t timeout);
+
+    virtual void onFrameAvailable();
+
+    int mPendingFrames;
+    Mutex mMutex;
+    Condition mCondition;
+};
+
+}
diff --git a/tests/keymaster/Android.mk b/tests/keymaster/Android.mk
new file mode 100644
index 0000000..2661211
--- /dev/null
+++ b/tests/keymaster/Android.mk
@@ -0,0 +1,29 @@
+# Build the keymaster unit tests
+
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= \
+    keymaster_test.cpp
+
+# Note that "bionic" is needed because of stlport
+LOCAL_C_INCLUDES := \
+    bionic \
+    external/gtest/include \
+    external/stlport/stlport
+
+LOCAL_SHARED_LIBRARIES := \
+    liblog \
+    libutils \
+    libstlport \
+    libhardware
+
+LOCAL_STATIC_LIBRARIES := \
+    libgtest \
+    libgtest_main
+
+LOCAL_MODULE := keymaster_test
+
+LOCAL_MODULE_TAGS := tests
+
+include $(BUILD_EXECUTABLE)
diff --git a/tests/keymaster/keymaster_test.cpp b/tests/keymaster/keymaster_test.cpp
new file mode 100644
index 0000000..f4cfcd2
--- /dev/null
+++ b/tests/keymaster/keymaster_test.cpp
@@ -0,0 +1,793 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "keymaster_test"
+#include <utils/Log.h>
+#include <utils/UniquePtr.h>
+
+#include <hardware/keymaster.h>
+
+#include <gtest/gtest.h>
+
+#include <fstream>
+#include <iostream>
+
+namespace android {
+
+class UniqueBlob : public UniquePtr<uint8_t[]> {
+public:
+    UniqueBlob(uint8_t* bytes, size_t length) :
+            UniquePtr<uint8_t[]>(bytes), mLength(length) {
+    }
+
+    bool operator==(const UniqueBlob &other) const {
+        if (other.length() != mLength) {
+            return false;
+        }
+
+        const uint8_t* mine = get();
+        const uint8_t* theirs = other.get();
+
+        for (size_t i = 0; i < mLength; i++) {
+            if (mine[i] != theirs[i]) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    size_t length() const {
+        return mLength;
+    }
+
+    friend std::ostream &operator<<(std::ostream &stream, const UniqueBlob& blob);
+
+private:
+    size_t mLength;
+};
+
+std::ostream &operator<<(std::ostream &stream, const UniqueBlob& blob) {
+    const size_t length = blob.mLength;
+    stream << "Blob length=" << length << " < ";
+
+    const uint8_t* data = blob.get();
+    for (size_t i = 0; i < length; i++) {
+        stream << std::hex << std::setw(2) << std::setfill('0')
+                << static_cast<unsigned int>(data[i]) << ' ';
+    }
+    stream << '>' << std::endl;
+
+    return stream;
+}
+
+class UniqueKey : public UniqueBlob {
+public:
+    UniqueKey(keymaster_device_t** dev, uint8_t* bytes, size_t length) :
+            UniqueBlob(bytes, length), mDevice(dev) {
+    }
+
+    ~UniqueKey() {
+        if (mDevice != NULL && *mDevice != NULL) {
+            keymaster_device_t* dev = *mDevice;
+            if (dev->delete_keypair != NULL) {
+                dev->delete_keypair(dev, get(), length());
+            }
+        }
+    }
+
+private:
+    keymaster_device_t** mDevice;
+};
+
+/*
+ * DER-encoded PKCS#8 format RSA key. Generated using:
+ *
+ * openssl genrsa 2048 | openssl pkcs8 -topk8 -nocrypt -outform der | recode ../x1
+ */
+static uint8_t TEST_KEY_1[] = {
+        0x30, 0x82, 0x04, 0xBE, 0x02, 0x01, 0x00, 0x30, 0x0D, 0x06, 0x09, 0x2A,
+        0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
+        0x04, 0xA8, 0x30, 0x82, 0x04, 0xA4, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01,
+        0x01, 0x00, 0xD8, 0x58, 0xD4, 0x9F, 0xC0, 0xE8, 0xF0, 0xFF, 0x87, 0x27,
+        0x43, 0xE6, 0x2E, 0xE6, 0x9A, 0x42, 0x3B, 0x39, 0x94, 0x84, 0x43, 0x55,
+        0x8D, 0x20, 0x5B, 0x71, 0x88, 0xE6, 0xD1, 0x62, 0xC8, 0xF2, 0x20, 0xD0,
+        0x75, 0x13, 0x83, 0xA3, 0x5D, 0x19, 0xA8, 0x62, 0xD0, 0x5F, 0x3E, 0x8A,
+        0x7C, 0x0E, 0x26, 0xA9, 0xFF, 0xB2, 0x5E, 0x63, 0xAA, 0x3C, 0x8D, 0x13,
+        0x41, 0xAA, 0xD5, 0x03, 0x01, 0x01, 0x53, 0xC9, 0x02, 0x1C, 0xEC, 0xE8,
+        0xC4, 0x70, 0x3F, 0x43, 0xE5, 0x51, 0xD0, 0x6E, 0x52, 0x0B, 0xC4, 0x0A,
+        0xA3, 0x61, 0xDE, 0xE3, 0x72, 0x0C, 0x94, 0xF1, 0x1C, 0x2D, 0x36, 0x77,
+        0xBB, 0x16, 0xA8, 0x63, 0x4B, 0xD1, 0x07, 0x00, 0x42, 0x2D, 0x2B, 0x10,
+        0x80, 0x45, 0xF3, 0x0C, 0xF9, 0xC5, 0xAC, 0xCC, 0x64, 0x87, 0xFD, 0x5D,
+        0xC8, 0x51, 0xD4, 0x1C, 0x9E, 0x6E, 0x9B, 0xC4, 0x27, 0x5E, 0x73, 0xA7,
+        0x2A, 0xF6, 0x90, 0x42, 0x0C, 0x34, 0x93, 0xB7, 0x02, 0x19, 0xA9, 0x64,
+        0x6C, 0x46, 0x3B, 0x40, 0x02, 0x2F, 0x54, 0x69, 0x79, 0x26, 0x7D, 0xF6,
+        0x85, 0x90, 0x01, 0xD0, 0x21, 0x07, 0xD0, 0x14, 0x00, 0x65, 0x9C, 0xAC,
+        0x24, 0xE8, 0x78, 0x42, 0x3B, 0x90, 0x75, 0x19, 0x55, 0x11, 0x4E, 0xD9,
+        0xE6, 0x97, 0x87, 0xBC, 0x8D, 0x2C, 0x9B, 0xF0, 0x1F, 0x14, 0xEB, 0x6A,
+        0x57, 0xCE, 0x78, 0xAD, 0xCE, 0xD9, 0xFB, 0xB9, 0xA1, 0xEF, 0x0C, 0x1F,
+        0xDD, 0xE3, 0x5B, 0x73, 0xA0, 0xEC, 0x37, 0x9C, 0xE1, 0xFD, 0x86, 0x28,
+        0xC3, 0x4A, 0x42, 0xD0, 0xA3, 0xFE, 0x57, 0x09, 0x29, 0xD8, 0xF6, 0xEC,
+        0xE3, 0xC0, 0x71, 0x7C, 0x29, 0x27, 0xC2, 0xD1, 0x3E, 0x22, 0xBC, 0xBD,
+        0x5A, 0x85, 0x41, 0xF6, 0x15, 0xDA, 0x0C, 0x58, 0x5A, 0x61, 0x5B, 0x78,
+        0xB8, 0xAA, 0xEC, 0x5C, 0x1C, 0x79, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02,
+        0x82, 0x01, 0x00, 0x1D, 0x10, 0x31, 0xE0, 0x14, 0x26, 0x36, 0xD9, 0xDC,
+        0xEA, 0x25, 0x70, 0xF2, 0xB3, 0xFF, 0xDD, 0x0D, 0xDF, 0xBA, 0x57, 0xDA,
+        0x43, 0xCF, 0xE5, 0x9C, 0xE3, 0x2F, 0xA4, 0xF2, 0x53, 0xF6, 0xF2, 0xAF,
+        0xFD, 0xD0, 0xFC, 0x82, 0x1E, 0x9C, 0x0F, 0x2A, 0x53, 0xBB, 0xF2, 0x4F,
+        0x90, 0x83, 0x01, 0xD3, 0xA7, 0xDA, 0xB5, 0xB7, 0x80, 0x64, 0x0A, 0x26,
+        0x59, 0x83, 0xE4, 0xD3, 0x20, 0xC8, 0x2D, 0xC9, 0x77, 0xA3, 0x55, 0x07,
+        0x6E, 0x6D, 0x95, 0x36, 0xAA, 0x84, 0x4F, 0xED, 0x54, 0x24, 0xA9, 0x77,
+        0xF8, 0x85, 0xE2, 0x4B, 0xF2, 0xFA, 0x0B, 0x3E, 0xA6, 0xF5, 0x46, 0x0D,
+        0x9F, 0x1F, 0xFE, 0xF7, 0x37, 0xFF, 0xA3, 0x60, 0xF1, 0x63, 0xF2, 0x75,
+        0x6A, 0x8E, 0x10, 0xD7, 0x89, 0xD2, 0xB3, 0xFF, 0x76, 0xA5, 0xBA, 0xAF,
+        0x0A, 0xBE, 0x32, 0x5F, 0xF0, 0x48, 0x48, 0x4B, 0x9C, 0x9A, 0x3D, 0x12,
+        0xA7, 0xD2, 0x07, 0xC7, 0x59, 0x32, 0x94, 0x95, 0x65, 0x2F, 0x87, 0x34,
+        0x76, 0xBA, 0x7C, 0x08, 0x4B, 0xAB, 0xA6, 0x24, 0xDF, 0x64, 0xDB, 0x48,
+        0x63, 0x42, 0x06, 0xE2, 0x2C, 0x3D, 0xFB, 0xE5, 0x47, 0x81, 0x94, 0x98,
+        0xF7, 0x32, 0x4B, 0x28, 0xEB, 0x42, 0xB8, 0xE9, 0x8E, 0xFC, 0xC9, 0x43,
+        0xC9, 0x47, 0xE6, 0xE7, 0x1C, 0xDC, 0x71, 0xEF, 0x4D, 0x8A, 0xB1, 0xFC,
+        0x45, 0x37, 0xEC, 0xB3, 0x16, 0x88, 0x5B, 0xE2, 0xEC, 0x8B, 0x6B, 0x75,
+        0x16, 0xBE, 0x6B, 0xF8, 0x2C, 0xF8, 0xC9, 0xD1, 0xF7, 0x55, 0x87, 0x57,
+        0x5F, 0xDE, 0xF4, 0x7E, 0x72, 0x13, 0x06, 0x2A, 0x21, 0xB7, 0x78, 0x21,
+        0x05, 0xFD, 0xE2, 0x5F, 0x7B, 0x7C, 0xF0, 0x26, 0x2B, 0x75, 0x7F, 0x68,
+        0xF9, 0xA6, 0x98, 0xFD, 0x54, 0x0E, 0xCC, 0x22, 0x41, 0x7F, 0x29, 0x81,
+        0x2F, 0xA3, 0x3C, 0x3D, 0x64, 0xC8, 0x41, 0x02, 0x81, 0x81, 0x00, 0xFA,
+        0xFA, 0xE4, 0x2E, 0x30, 0xF0, 0x7A, 0x8D, 0x95, 0xB8, 0x39, 0x58, 0x27,
+        0x0F, 0x89, 0x0C, 0xDF, 0xFE, 0x2F, 0x55, 0x3B, 0x6F, 0xDD, 0x5F, 0x12,
+        0xB3, 0xD1, 0xCF, 0x5B, 0x8D, 0xB6, 0x10, 0x1C, 0x87, 0x0C, 0x30, 0x89,
+        0x2D, 0xBB, 0xB8, 0xA1, 0x78, 0x0F, 0x54, 0xA6, 0x36, 0x46, 0x05, 0x8B,
+        0x5A, 0xFF, 0x48, 0x03, 0x13, 0xAE, 0x95, 0x96, 0x5D, 0x6C, 0xDA, 0x5D,
+        0xF7, 0xAD, 0x1D, 0x33, 0xED, 0x23, 0xF5, 0x4B, 0x03, 0x78, 0xE7, 0x50,
+        0xD1, 0x2D, 0x95, 0x22, 0x35, 0x02, 0x5B, 0x4A, 0x4E, 0x73, 0xC9, 0xB7,
+        0x05, 0xC4, 0x21, 0x86, 0x1F, 0x1E, 0x40, 0x83, 0xBC, 0x8A, 0x3A, 0x95,
+        0x24, 0x62, 0xF4, 0x58, 0x38, 0x64, 0x4A, 0x89, 0x8A, 0x27, 0x59, 0x12,
+        0x9D, 0x21, 0xC3, 0xA6, 0x42, 0x1E, 0x2A, 0x3F, 0xD8, 0x65, 0x1F, 0x6E,
+        0x3E, 0x4D, 0x5C, 0xCC, 0xEA, 0x8E, 0x15, 0x02, 0x81, 0x81, 0x00, 0xDC,
+        0xAC, 0x9B, 0x00, 0xDB, 0xF9, 0xB2, 0xBF, 0xC4, 0x5E, 0xB6, 0xB7, 0x63,
+        0xEB, 0x13, 0x4B, 0xE2, 0xA6, 0xC8, 0x72, 0x90, 0xD8, 0xC2, 0x33, 0x33,
+        0xF0, 0x66, 0x75, 0xBD, 0x50, 0x7C, 0xA4, 0x8F, 0x82, 0xFB, 0xFF, 0x44,
+        0x3B, 0xE7, 0x15, 0x3A, 0x0C, 0x7A, 0xF8, 0x92, 0x86, 0x4A, 0x79, 0x32,
+        0x08, 0x82, 0x1D, 0x6A, 0xBA, 0xAD, 0x8A, 0xB3, 0x3D, 0x7F, 0xA5, 0xB4,
+        0x6F, 0x67, 0x86, 0x7E, 0xB2, 0x9C, 0x2A, 0xF6, 0x7C, 0x49, 0x21, 0xC5,
+        0x3F, 0x00, 0x3F, 0x9B, 0xF7, 0x0F, 0x6C, 0x35, 0x80, 0x75, 0x73, 0xC0,
+        0xF8, 0x3E, 0x30, 0x5F, 0x74, 0x2F, 0x15, 0x41, 0xEA, 0x0F, 0xCE, 0x0E,
+        0x18, 0x17, 0x68, 0xBA, 0xC4, 0x29, 0xF2, 0xE2, 0x2C, 0x1D, 0x55, 0x83,
+        0xB6, 0x64, 0x2E, 0x03, 0x12, 0xA4, 0x0D, 0xBF, 0x4F, 0x2E, 0xBE, 0x7C,
+        0x41, 0xD9, 0xCD, 0xD0, 0x52, 0x91, 0xD5, 0x02, 0x81, 0x81, 0x00, 0xD4,
+        0x55, 0xEB, 0x32, 0xC1, 0x28, 0xD3, 0x26, 0x72, 0x22, 0xB8, 0x31, 0x42,
+        0x6A, 0xBC, 0x52, 0x6E, 0x37, 0x48, 0xA8, 0x5D, 0x6E, 0xD8, 0xE5, 0x14,
+        0x97, 0x99, 0xCC, 0x4A, 0xF2, 0xEB, 0xB3, 0x59, 0xCF, 0x4F, 0x9A, 0xC8,
+        0x94, 0x2E, 0x9B, 0x97, 0xD0, 0x51, 0x78, 0x16, 0x5F, 0x18, 0x82, 0x9C,
+        0x51, 0xD2, 0x64, 0x84, 0x65, 0xE4, 0x70, 0x9E, 0x14, 0x50, 0x81, 0xB6,
+        0xBA, 0x52, 0x75, 0xC0, 0x76, 0xC2, 0xD3, 0x46, 0x31, 0x9B, 0xDA, 0x67,
+        0xDF, 0x71, 0x27, 0x19, 0x17, 0xAB, 0xF4, 0xBC, 0x3A, 0xFF, 0x6F, 0x0B,
+        0x2F, 0x0F, 0xAE, 0x25, 0x20, 0xB2, 0xA1, 0x76, 0x52, 0xCE, 0xC7, 0x9D,
+        0x62, 0x79, 0x6D, 0xAC, 0x2D, 0x99, 0x7C, 0x0E, 0x3D, 0x19, 0xE9, 0x1B,
+        0xFC, 0x60, 0x92, 0x7C, 0x58, 0xB7, 0xD8, 0x9A, 0xC7, 0x63, 0x56, 0x62,
+        0x18, 0xC7, 0xAE, 0xD9, 0x97, 0x1F, 0xB9, 0x02, 0x81, 0x81, 0x00, 0x91,
+        0x40, 0xC4, 0x1E, 0x82, 0xAD, 0x0F, 0x6D, 0x8E, 0xD2, 0x51, 0x2E, 0xD1,
+        0x84, 0x30, 0x85, 0x68, 0xC1, 0x23, 0x7B, 0xD5, 0xBF, 0xF7, 0xC4, 0x40,
+        0x51, 0xE2, 0xFF, 0x69, 0x07, 0x8B, 0xA3, 0xBE, 0x1B, 0x17, 0xC8, 0x64,
+        0x9F, 0x91, 0x71, 0xB5, 0x6D, 0xF5, 0x9B, 0x9C, 0xC6, 0xEC, 0x4A, 0x6E,
+        0x16, 0x8F, 0x9E, 0xD1, 0x5B, 0xE3, 0x53, 0x42, 0xBC, 0x1E, 0x43, 0x72,
+        0x4B, 0x4A, 0x37, 0x8B, 0x3A, 0x01, 0xF5, 0x7D, 0x9D, 0x3D, 0x7E, 0x0F,
+        0x19, 0x73, 0x0E, 0x6B, 0x98, 0xE9, 0xFB, 0xEE, 0x13, 0x8A, 0x3C, 0x11,
+        0x2E, 0xD5, 0xB0, 0x7D, 0x84, 0x3A, 0x61, 0xA1, 0xAB, 0x71, 0x8F, 0xCE,
+        0x53, 0x29, 0x45, 0x74, 0x7A, 0x1E, 0xAA, 0x93, 0x19, 0x3A, 0x8D, 0xC9,
+        0x4E, 0xCB, 0x0E, 0x46, 0x53, 0x84, 0xCC, 0xCF, 0xBA, 0x4D, 0x28, 0x71,
+        0x1D, 0xDF, 0x41, 0xCB, 0xF8, 0x2D, 0xA9, 0x02, 0x81, 0x80, 0x04, 0x8B,
+        0x4A, 0xEA, 0xBD, 0x39, 0x0B, 0x96, 0xC5, 0x1D, 0xA4, 0x47, 0xFD, 0x46,
+        0xD2, 0x8A, 0xEA, 0x2A, 0xF3, 0x9D, 0x3A, 0x7E, 0x16, 0x74, 0xFC, 0x13,
+        0xDE, 0x4D, 0xA9, 0x85, 0x42, 0x33, 0x02, 0x92, 0x0B, 0xB6, 0xDB, 0x7E,
+        0xEA, 0x85, 0xC2, 0x94, 0x43, 0x52, 0x37, 0x5A, 0x77, 0xAB, 0xCB, 0x61,
+        0x88, 0xDE, 0xF8, 0xFA, 0xDB, 0xE8, 0x0B, 0x95, 0x7D, 0x39, 0x19, 0xA2,
+        0x89, 0xB9, 0x32, 0xB2, 0x50, 0x38, 0xF7, 0x88, 0x69, 0xFD, 0xA4, 0x63,
+        0x1F, 0x9B, 0x03, 0xD8, 0xA6, 0x7A, 0x05, 0x76, 0x02, 0x28, 0x93, 0x82,
+        0x73, 0x7F, 0x14, 0xCC, 0xBE, 0x29, 0x10, 0xAD, 0x8A, 0x2E, 0xAC, 0xED,
+        0x11, 0xA7, 0x72, 0x7C, 0x60, 0x78, 0x72, 0xFB, 0x78, 0x20, 0x18, 0xC9,
+        0x7E, 0x63, 0xAD, 0x55, 0x54, 0x51, 0xDB, 0x9F, 0x7B, 0xD4, 0x8F, 0xB2,
+        0xDE, 0x3B, 0xF1, 0x70, 0x23, 0xE5,
+};
+
+/*
+ * Generated using keys on the keyboard and lack of imagination.
+ */
+static unsigned char BOGUS_KEY_1[] = { 0xFF, 0xFF, 0xFF, 0xFF };
+
+
+class KeymasterTest : public testing::Test {
+protected:
+    static void SetUpTestCase() {
+        const hw_module_t* mod;
+        ASSERT_EQ(0, hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &mod))
+                << "Should be able to find a keymaster hardware module";
+
+        std::cout << "Using keymaster module: " << mod->name << std::endl;
+
+        ASSERT_EQ(0, keymaster_open(mod, &sDevice))
+                << "Should be able to open the keymaster device";
+
+        ASSERT_TRUE(sDevice->generate_keypair != NULL)
+                << "Should implement generate_keypair";
+
+        ASSERT_TRUE(sDevice->import_keypair != NULL)
+                << "Should implement import_keypair";
+
+        ASSERT_TRUE(sDevice->get_keypair_public != NULL)
+                << "Should implement get_keypair_public";
+
+        ASSERT_TRUE(sDevice->sign_data != NULL)
+                << "Should implement sign_data";
+
+        ASSERT_TRUE(sDevice->verify_data != NULL)
+                << "Should implement verify_data";
+    }
+
+    static void TearDownTestCase() {
+        ASSERT_EQ(0, keymaster_close(sDevice));
+    }
+
+    virtual void SetUp() {
+    }
+
+    virtual void TearDown() {
+    }
+
+    static keymaster_device_t* sDevice;
+};
+
+keymaster_device_t* KeymasterTest::sDevice = NULL;
+
+TEST_F(KeymasterTest, GenerateKeyPair_RSA_512_Success) {
+    keymaster_keypair_t key_type = TYPE_RSA;
+    keymaster_rsa_keygen_params_t params = {
+            modulus_size: 512,
+            public_exponent: 0x10001L,
+    };
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->generate_keypair(sDevice, key_type, &params, &key_blob, &key_blob_length))
+            << "Should generate an RSA key with 512 bit modulus size";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, GenerateKeyPair_RSA_1024_Success) {
+    keymaster_keypair_t key_type = TYPE_RSA;
+    keymaster_rsa_keygen_params_t params = {
+            modulus_size: 1024,
+            public_exponent: 0x3L,
+    };
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->generate_keypair(sDevice, key_type, &params, &key_blob, &key_blob_length))
+            << "Should generate an RSA key with 2048 bit modulus size";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, GenerateKeyPair_RSA_2048_Success) {
+    keymaster_keypair_t key_type = TYPE_RSA;
+    keymaster_rsa_keygen_params_t params = {
+            modulus_size: 2048,
+            public_exponent: 0x3L,
+    };
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->generate_keypair(sDevice, key_type, &params, &key_blob, &key_blob_length))
+            << "Should generate an RSA key with 2048 bit modulus size";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, GenerateKeyPair_RSA_NullParams_Failure) {
+    keymaster_keypair_t key_type = TYPE_RSA;
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(-1,
+            sDevice->generate_keypair(sDevice, key_type, NULL, &key_blob, &key_blob_length))
+            << "Should not be able to generate an RSA key with null params";
+}
+
+TEST_F(KeymasterTest, GenerateKeyPair_UnknownType_Failure) {
+    keymaster_keypair_t key_type = static_cast<keymaster_keypair_t>(0xFFFF);
+
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(-1,
+            sDevice->generate_keypair(sDevice, key_type, NULL, &key_blob, &key_blob_length))
+            << "Should not generate an unknown key type";
+}
+
+TEST_F(KeymasterTest, ImportKeyPair_RSA_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, ImportKeyPair_BogusKey_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(-1,
+            sDevice->import_keypair(sDevice, BOGUS_KEY_1, sizeof(BOGUS_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should not import an unknown key type";
+}
+
+TEST_F(KeymasterTest, ImportKeyPair_NullKey_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(-1,
+            sDevice->import_keypair(sDevice, NULL, 0,
+                    &key_blob, &key_blob_length))
+            << "Should not import a null key";
+}
+
+TEST_F(KeymasterTest, GetKeypairPublic_RSA_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    uint8_t* x509_data;
+    size_t x509_data_length;
+    EXPECT_EQ(0,
+            sDevice->get_keypair_public(sDevice, key_blob, key_blob_length,
+                    &x509_data, &x509_data_length))
+            << "Should be able to retrieve RSA public key successfully";
+    UniqueBlob x509_blob(x509_data, x509_data_length);
+}
+
+TEST_F(KeymasterTest, GetKeypairPublic_RSA_NullKey_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    uint8_t* x509_data = NULL;
+    size_t x509_data_length;
+    EXPECT_EQ(-1,
+            sDevice->get_keypair_public(sDevice, NULL, 0,
+                    &x509_data, &x509_data_length))
+            << "Should not be able to retrieve RSA public key from null key";
+    UniqueBlob x509_blob(x509_data, x509_data_length);
+}
+
+TEST_F(KeymasterTest, GetKeypairPublic_RSA_NullDestination_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    EXPECT_EQ(-1,
+            sDevice->get_keypair_public(sDevice, key.get(), key.length(),
+                    NULL, NULL))
+            << "Should not be able to succeed with NULL destination blob";
+}
+
+TEST_F(KeymasterTest, DeleteKeyPair_RSA_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    EXPECT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+}
+
+TEST_F(KeymasterTest, DeleteKeyPair_RSA_DoubleDelete_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    /*
+     * This is only run if the module indicates it implements key deletion
+     * by implementing delete_keypair.
+     */
+    if (sDevice->delete_keypair != NULL) {
+        ASSERT_EQ(0,
+                sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                        &key_blob, &key_blob_length))
+                << "Should successfully import an RSA key";
+        UniqueBlob blob(key_blob, key_blob_length);
+
+        EXPECT_EQ(0, sDevice->delete_keypair(sDevice, key_blob, key_blob_length))
+                << "Should delete key after import";
+
+        EXPECT_EQ(-1, sDevice->delete_keypair(sDevice, key_blob, key_blob_length))
+                << "Should not be able to delete key twice";
+    }
+}
+
+TEST_F(KeymasterTest, DeleteKeyPair_RSA_NullKey_Failure) {
+    /*
+     * This is only run if the module indicates it implements key deletion
+     * by implementing delete_keypair.
+     */
+    if (sDevice->delete_keypair != NULL) {
+        EXPECT_EQ(-1, sDevice->delete_keypair(sDevice, NULL, 0))
+                << "Should not be able to delete null key";
+    }
+}
+
+/*
+ * DER-encoded PKCS#8 format RSA key. Generated using:
+ *
+ * openssl genrsa 512 | openssl pkcs8 -topk8 -nocrypt -outform der | recode ../x1
+ */
+static uint8_t TEST_SIGN_KEY_1[] = {
+        0x30, 0x82, 0x01, 0x56, 0x02, 0x01, 0x00, 0x30, 0x0D, 0x06, 0x09, 0x2A,
+        0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
+        0x01, 0x40, 0x30, 0x82, 0x01, 0x3C, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00,
+        0xBD, 0xC0, 0x7F, 0xEF, 0x75, 0x1D, 0x63, 0x2A, 0xD0, 0x9A, 0x26, 0xE5,
+        0x5B, 0xB9, 0x84, 0x7C, 0xE5, 0xC7, 0xE7, 0xDE, 0xFE, 0xB6, 0x54, 0xD9,
+        0xF0, 0x9B, 0xC2, 0xCF, 0x36, 0xDA, 0xE5, 0x4D, 0xC5, 0xD9, 0x25, 0x78,
+        0xBD, 0x55, 0x05, 0xBD, 0x86, 0xFB, 0x37, 0x15, 0x33, 0x42, 0x52, 0xED,
+        0xE5, 0xCD, 0xCB, 0xB7, 0xA2, 0x51, 0xFA, 0x36, 0xE9, 0x9C, 0x2E, 0x5D,
+        0xE3, 0xA5, 0x1F, 0x01, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x41, 0x00,
+        0x96, 0x71, 0xDE, 0xBD, 0x83, 0x94, 0x96, 0x40, 0xA6, 0xFD, 0xE1, 0xA2,
+        0xED, 0xD3, 0xAC, 0x28, 0xBE, 0xA2, 0x7D, 0xC3, 0xFF, 0x1D, 0x9F, 0x2E,
+        0xE0, 0xA7, 0x0E, 0x90, 0xEE, 0x44, 0x25, 0x92, 0xE3, 0x54, 0xDD, 0x55,
+        0xA3, 0xEF, 0x42, 0xF5, 0x52, 0x55, 0x41, 0x47, 0x5E, 0x00, 0xFB, 0x8B,
+        0x47, 0x5E, 0x45, 0x49, 0xEA, 0x3D, 0x2C, 0xFD, 0x9F, 0xEC, 0xC8, 0x4E,
+        0x4E, 0x86, 0x90, 0x31, 0x02, 0x21, 0x00, 0xE6, 0xA5, 0x55, 0xB3, 0x64,
+        0xAB, 0x90, 0x5E, 0xA2, 0xF5, 0x6B, 0x21, 0x4B, 0x15, 0xD6, 0x4A, 0xB6,
+        0x60, 0x24, 0x95, 0x65, 0xA2, 0xBE, 0xBA, 0x2A, 0x73, 0xFB, 0xFF, 0x2C,
+        0x61, 0x88, 0x9D, 0x02, 0x21, 0x00, 0xD2, 0x9C, 0x5B, 0xFE, 0x82, 0xA5,
+        0xFC, 0x52, 0x6A, 0x29, 0x38, 0xDB, 0x22, 0x3B, 0xEB, 0x74, 0x3B, 0xCA,
+        0xB4, 0xDD, 0x1D, 0xE4, 0x48, 0x60, 0x70, 0x19, 0x9B, 0x81, 0xC1, 0x83,
+        0x28, 0xB5, 0x02, 0x21, 0x00, 0x89, 0x2D, 0xFE, 0xF9, 0xF2, 0xBF, 0x43,
+        0xDF, 0xB5, 0xA6, 0xA8, 0x30, 0x26, 0x1B, 0x77, 0xD7, 0xF9, 0xFE, 0xD6,
+        0xE3, 0x70, 0x8E, 0xCA, 0x47, 0xA9, 0xA6, 0x50, 0x54, 0x25, 0xCE, 0x60,
+        0xD5, 0x02, 0x21, 0x00, 0xBE, 0x5A, 0xF8, 0x82, 0xE6, 0xCE, 0xE3, 0x6A,
+        0x11, 0xED, 0xC4, 0x27, 0xBB, 0x9F, 0x70, 0xC6, 0x93, 0xAC, 0x39, 0x20,
+        0x89, 0x7D, 0xE5, 0x34, 0xD4, 0xDD, 0x30, 0x42, 0x6D, 0x07, 0x00, 0xE9,
+        0x02, 0x20, 0x05, 0x91, 0xEF, 0x12, 0xD2, 0xD3, 0x6A, 0xD2, 0x96, 0x6B,
+        0x10, 0x62, 0xF9, 0xBA, 0xA4, 0x91, 0x48, 0x84, 0x40, 0x61, 0x67, 0x80,
+        0x68, 0x68, 0xC8, 0x60, 0xB3, 0x66, 0xC8, 0xF9, 0x08, 0x9A,
+};
+
+/*
+ * PKCS#1 v1.5 padded raw "Hello, world"  Can be generated be generated by verifying
+ * the signature below in no padding mode:
+ *
+ * openssl rsautl -keyform der -inkey rsa.der -raw -verify -in test.sig
+ */
+static uint8_t TEST_SIGN_DATA_1[] = {
+        0x00, 0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+        0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+        0xFF, 0xFF, 0xFF, 0x00, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2C, 0x20, 0x77,
+        0x6F, 0x72, 0x6C, 0x64,
+};
+
+/*
+ * Signature of TEST_SIGN_DATA_1 using TEST_SIGN_KEY_1. Generated using:
+ *
+ * echo 'Hello, world' | openssl rsautl -keyform der -inkey rsa.der -sign | recode ../x1
+ */
+static uint8_t TEST_SIGN_SIGNATURE_1[] = {
+        0xA4, 0xBB, 0x76, 0x87, 0xFE, 0x61, 0x0C, 0x9D, 0xD6, 0xFF, 0x4B, 0x76,
+        0x96, 0x08, 0x36, 0x23, 0x11, 0xC6, 0x44, 0x3F, 0x88, 0x77, 0x97, 0xB2,
+        0xA8, 0x3B, 0xFB, 0x9C, 0x3C, 0xD3, 0x20, 0x65, 0xFD, 0x26, 0x3B, 0x2A,
+        0xB8, 0xB6, 0xD4, 0xDC, 0x91, 0xF7, 0xE2, 0xDE, 0x4D, 0xF7, 0x0E, 0xB9,
+        0x72, 0xA7, 0x29, 0x72, 0x82, 0x12, 0x7C, 0x53, 0x23, 0x21, 0xC4, 0xFF,
+        0x79, 0xE4, 0x91, 0x40,
+};
+
+/*
+ * Identical to TEST_SIGN_SIGNATURE_1 except the last octet is '1' instead of '0'
+ * This should fail any test.
+ */
+static uint8_t TEST_SIGN_SIGNATURE_BOGUS_1[] = {
+        0xA4, 0xBB, 0x76, 0x87, 0xFE, 0x61, 0x0C, 0x9D, 0xD6, 0xFF, 0x4B, 0x76,
+        0x96, 0x08, 0x36, 0x23, 0x11, 0xC6, 0x44, 0x3F, 0x88, 0x77, 0x97, 0xB2,
+        0xA8, 0x3B, 0xFB, 0x9C, 0x3C, 0xD3, 0x20, 0x65, 0xFD, 0x26, 0x3B, 0x2A,
+        0xB8, 0xB6, 0xD4, 0xDC, 0x91, 0xF7, 0xE2, 0xDE, 0x4D, 0xF7, 0x0E, 0xB9,
+        0x72, 0xA7, 0x29, 0x72, 0x82, 0x12, 0x7C, 0x53, 0x23, 0x21, 0xC4, 0xFF,
+        0x79, 0xE4, 0x91, 0x41,
+};
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(0,
+            sDevice->sign_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    &sig, &sig_length))
+            << "Should sign data successfully";
+    UniqueBlob sig_blob(sig, sig_length);
+
+    UniqueBlob expected_sig(TEST_SIGN_SIGNATURE_1, sizeof(TEST_SIGN_SIGNATURE_1));
+
+    EXPECT_EQ(expected_sig, sig_blob)
+            << "Generated signature should match expected signature";
+
+    // The expected signature is actually stack data, so don't let it try to free.
+    uint8_t* unused __attribute__((unused)) = expected_sig.release();
+}
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_InvalidSizeInput_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(-1,
+            sDevice->sign_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &sig, &sig_length))
+            << "Should not be able to do raw signature on incorrect size data";
+}
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_NullKey_Failure) {
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(-1,
+            sDevice->sign_data(sDevice, &params, NULL, 0,
+                    TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &sig, &sig_length))
+            << "Should not be able to do raw signature on incorrect size data";
+}
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_NullInput_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(-1,
+            sDevice->sign_data(sDevice, &params, key_blob, key_blob_length,
+                    NULL, 0,
+                    &sig, &sig_length))
+            << "Should error when input data is null";
+}
+
+TEST_F(KeymasterTest, SignData_RSA_Raw_NullOutput_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    uint8_t* sig;
+    size_t sig_length;
+
+    EXPECT_EQ(-1,
+            sDevice->sign_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_KEY_1, sizeof(TEST_KEY_1),
+                    NULL, NULL))
+            << "Should error when output is null";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_Raw_Success) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(0,
+            sDevice->verify_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    TEST_SIGN_SIGNATURE_1, sizeof(TEST_SIGN_SIGNATURE_1)))
+            << "Should verify data successfully";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_Raw_BadSignature_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(-1,
+            sDevice->verify_data(sDevice, &params, key_blob, key_blob_length,
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    TEST_SIGN_SIGNATURE_BOGUS_1, sizeof(TEST_SIGN_SIGNATURE_BOGUS_1)))
+            << "Should sign data successfully";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_Raw_NullKey_Failure) {
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(-1,
+            sDevice->verify_data(sDevice, &params, NULL, 0,
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    TEST_SIGN_SIGNATURE_BOGUS_1, sizeof(TEST_SIGN_SIGNATURE_BOGUS_1)))
+            << "Should fail when key is null";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_NullInput_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(-1,
+            sDevice->verify_data(sDevice, &params, key_blob, key_blob_length,
+                    NULL, 0,
+                    TEST_SIGN_SIGNATURE_1, sizeof(TEST_SIGN_SIGNATURE_1)))
+            << "Should fail on null input";
+}
+
+TEST_F(KeymasterTest, VerifyData_RSA_NullSignature_Failure) {
+    uint8_t* key_blob;
+    size_t key_blob_length;
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key_blob, &key_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key(&sDevice, key_blob, key_blob_length);
+
+    keymaster_rsa_sign_params_t params = {
+            digest_type: DIGEST_NONE,
+            padding_type: PADDING_NONE,
+    };
+
+    EXPECT_EQ(-1,
+            sDevice->verify_data(sDevice, &params, key.get(), key.length(),
+                    TEST_SIGN_DATA_1, sizeof(TEST_SIGN_DATA_1),
+                    NULL, 0))
+            << "Should fail on null signature";
+}
+
+TEST_F(KeymasterTest, EraseAll_Success) {
+    uint8_t *key1_blob, *key2_blob;
+    size_t key1_blob_length, key2_blob_length;
+
+    // Only test this if the device says it supports delete_all
+    if (sDevice->delete_all == NULL) {
+        return;
+    }
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_SIGN_KEY_1, sizeof(TEST_SIGN_KEY_1),
+                    &key1_blob, &key1_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key1(&sDevice, key1_blob, key1_blob_length);
+
+    ASSERT_EQ(0,
+            sDevice->import_keypair(sDevice, TEST_KEY_1, sizeof(TEST_KEY_1),
+                    &key2_blob, &key2_blob_length))
+            << "Should successfully import an RSA key";
+    UniqueKey key2(&sDevice, key2_blob, key2_blob_length);
+
+    EXPECT_EQ(0, sDevice->delete_all(sDevice))
+            << "Should erase all keys";
+
+    key1.reset();
+
+    uint8_t* x509_data;
+    size_t x509_data_length;
+    ASSERT_EQ(-1,
+            sDevice->get_keypair_public(sDevice, key1_blob, key1_blob_length,
+                    &x509_data, &x509_data_length))
+            << "Should be able to retrieve RSA public key 1 successfully";
+
+    ASSERT_EQ(-1,
+            sDevice->get_keypair_public(sDevice, key2_blob, key2_blob_length,
+                    &x509_data, &x509_data_length))
+            << "Should be able to retrieve RSA public key 2 successfully";
+}
+
+}