Merge "Initial implementation of usb audio I/O"
diff --git a/include/hardware/audio_alsaops.h b/include/hardware/audio_alsaops.h
new file mode 100644
index 0000000..0d266ff
--- /dev/null
+++ b/include/hardware/audio_alsaops.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2014 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 file contains shared utility functions to handle the tinyalsa
+ * implementation for Android internal audio, generally in the hardware layer.
+ * Some routines may log a fatal error on failure, as noted.
+ */
+
+#ifndef ANDROID_AUDIO_ALSAOPS_H
+#define ANDROID_AUDIO_ALSAOPS_H
+
+#include <cutils/log.h>
+#include <system/audio.h>
+#include <tinyalsa/asoundlib.h>
+
+__BEGIN_DECLS
+
+/* Converts audio_format to pcm_format.
+ * Parameters:
+ *  format  the audio_format_t to convert
+ *
+ * Logs a fatal error if format is not a valid convertible audio_format_t.
+ */
+static inline enum pcm_format pcm_format_from_audio_format(audio_format_t format)
+{
+    switch (format) {
+#ifdef HAVE_BIG_ENDIAN
+    case AUDIO_FORMAT_PCM_16_BIT:
+        return PCM_FORMAT_S16_BE;
+    case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+        return PCM_FORMAT_S24_3BE;
+    case AUDIO_FORMAT_PCM_32_BIT:
+        return PCM_FORMAT_S32_BE;
+    case AUDIO_FORMAT_PCM_8_24_BIT:
+        return PCM_FORMAT_S24_BE;
+#else
+    case AUDIO_FORMAT_PCM_16_BIT:
+        return PCM_FORMAT_S16_LE;
+    case AUDIO_FORMAT_PCM_24_BIT_PACKED:
+        return PCM_FORMAT_S24_3LE;
+    case AUDIO_FORMAT_PCM_32_BIT:
+        return PCM_FORMAT_S32_LE;
+    case AUDIO_FORMAT_PCM_8_24_BIT:
+        return PCM_FORMAT_S24_LE;
+#endif
+    case AUDIO_FORMAT_PCM_FLOAT:  /* there is no equivalent for float */
+    default:
+        LOG_ALWAYS_FATAL("pcm_format_from_audio_format: invalid audio format %#x", format);
+        return 0;
+    }
+}
+
+/* Converts pcm_format to audio_format.
+ * Parameters:
+ *  format  the pcm_format to convert
+ *
+ * Logs a fatal error if format is not a valid convertible pcm_format.
+ */
+static inline audio_format_t audio_format_from_pcm_format(enum pcm_format format)
+{
+    switch (format) {
+#ifdef HAVE_BIG_ENDIAN
+    case PCM_FORMAT_S16_BE:
+        return AUDIO_FORMAT_PCM_16_BIT;
+    case PCM_FORMAT_S24_3BE:
+        return AUDIO_FORMAT_PCM_24_BIT_PACKED;
+    case PCM_FORMAT_S24_BE:
+        return AUDIO_FORMAT_PCM_8_24_BIT;
+    case PCM_FORMAT_S32_BE:
+        return AUDIO_FORMAT_PCM_32_BIT;
+#else
+    case PCM_FORMAT_S16_LE:
+        return AUDIO_FORMAT_PCM_16_BIT;
+    case PCM_FORMAT_S24_3LE:
+        return AUDIO_FORMAT_PCM_24_BIT_PACKED;
+    case PCM_FORMAT_S24_LE:
+        return AUDIO_FORMAT_PCM_8_24_BIT;
+    case PCM_FORMAT_S32_LE:
+        return AUDIO_FORMAT_PCM_32_BIT;
+#endif
+    default:
+        LOG_ALWAYS_FATAL("audio_format_from_pcm_format: invalid pcm format %#x", format);
+        return 0;
+    }
+}
+
+__END_DECLS
+
+#endif /* ANDROID_AUDIO_ALSAOPS_H */
diff --git a/include/hardware/bt_gatt_client.h b/include/hardware/bt_gatt_client.h
index cf4fabe..11b146d 100644
--- a/include/hardware/bt_gatt_client.h
+++ b/include/hardware/bt_gatt_client.h
@@ -156,6 +156,9 @@
  */
 typedef void (*listen_callback)(int status, int server_if);
 
+/** Callback invoked when the MTU for a given connection changes */
+typedef void (*configure_mtu_callback)(int conn_id, int status, int mtu);
+
 typedef struct {
     register_client_callback            register_client_cb;
     scan_result_callback                scan_result_cb;
@@ -175,6 +178,7 @@
     execute_write_callback              execute_write_cb;
     read_remote_rssi_callback           read_remote_rssi_cb;
     listen_callback                     listen_cb;
+    configure_mtu_callback              configure_mtu_cb;
 } btgatt_client_callbacks_t;
 
 /** Represents the standard BT-GATT client interface. */
@@ -282,6 +286,9 @@
                     uint16_t service_data_len, char* service_data,
                     uint16_t service_uuid_len, char* service_uuid);
 
+    /** Configure the MTU for a given connection */
+    bt_status_t (*configure_mtu)(int conn_id, int mtu);
+
     /** Test mode interface */
     bt_status_t (*test_command)( int command, btgatt_test_params_t* params);
 } btgatt_client_interface_t;
diff --git a/include/hardware/camera3.h b/include/hardware/camera3.h
index 9eb52ed..4503885 100644
--- a/include/hardware/camera3.h
+++ b/include/hardware/camera3.h
@@ -52,6 +52,7 @@
  *   S6. Error management
  *   S7. Key Performance Indicator (KPI) glossary
  *   S8. Sample Use Cases
+ *   S9. Notes on Controls and Metadata
  */
 
 /**
@@ -1139,6 +1140,32 @@
  *
  */
 
+/**
+ *   S9. Notes on Controls and Metadata
+ *
+ *   This section contains notes about the interpretation and usage of various metadata tags.
+ *
+ *   S9.1 HIGH_QUALITY and FAST modes.
+ *
+ *   Many camera post-processing blocks may be listed as having HIGH_QUALITY,
+ *   FAST, and OFF operating modes. These blocks will typically also have an
+ *   'available modes' tag representing which of these operating modes are
+ *   available on a given device. The general policy regarding implementing
+ *   these modes is as follows:
+ *
+ *   1. Operating mode controls of hardware blocks that cannot be disabled
+ *      must not list OFF in their corresponding 'available modes' tags.
+ *
+ *   2. OFF will always be included in their corresponding 'available modes'
+ *      tag if it is possible to disable that hardware block.
+ *
+ *   3. FAST must always be included in the 'available modes' tags for all
+ *      post-processing blocks supported on the device.  If a post-processing
+ *      block also has a slower and higher quality operating mode that does
+ *      not meet the framerate requirements for FAST mode, HIGH_QUALITY should
+ *      be included in the 'available modes' tag to represent this operating
+ *      mode.
+ */
 __BEGIN_DECLS
 
 struct camera3_device;
diff --git a/include/hardware/hdmi_cec.h b/include/hardware/hdmi_cec.h
index a697334..de6c70c 100644
--- a/include/hardware/hdmi_cec.h
+++ b/include/hardware/hdmi_cec.h
@@ -34,6 +34,7 @@
 #define HDMI_CEC_HARDWARE_INTERFACE "hdmi_cec_hw_if"
 
 typedef enum cec_device_type {
+    CEC_DEVICE_INACTIVE = -1,
     CEC_DEVICE_TV = 0,
     CEC_DEVICE_RECORDER = 1,
     CEC_DEVICE_RESERVED = 2,
@@ -127,7 +128,20 @@
     CEC_MESSAGE_GET_CEC_VERSION = 0x9F,
     CEC_MESSAGE_VENDOR_COMMAND_WITH_ID = 0xA0,
     CEC_MESSAGE_CLEAR_EXTERNAL_TIMER = 0xA1,
-    CEC_MESSAGE_SET_EXTERNAL_TIMER = 0xA2
+    CEC_MESSAGE_SET_EXTERNAL_TIMER = 0xA2,
+    CEC_MESSAGE_ABORT = 0xFF
+};
+
+/*
+ * Operand description [Abort Reason]
+ */
+enum abort_reason {
+    ABORT_UNRECOGNIZED_MODE = 0,
+    ABORT_NOT_IN_CORRECT_MODE = 1,
+    ABORT_CANNOT_PROVIDE_SOURCE = 2,
+    ABORT_INVALID_OPERAND = 3,
+    ABORT_REFUSED = 4,
+    ABORT_UNABLE_TO_DETERMINE = 5
 };
 
 /*
@@ -210,13 +224,15 @@
      * (*allocate_logical_address)() allocates a new logical address
      * for a given device type. The address is written to addr. The HAL
      * implementation is also expected to configure itself to start receiving
-     * the messages addressed to the allocated one. If allocation
-     * is not successful the addr will be set to CEC_ADDR_UNREGISTERED.
+     * the messages addressed to the allocated one. If the address has been already
+     * allocated, it should simply return the allocated address without attempting
+     * the allocation again. If allocation is not successful the addr will be
+     * set to CEC_ADDR_UNREGISTERED.
      *
      * Returns 0 on success or -errno on error.
      */
     int (*allocate_logical_address)(const struct hdmi_cec_device* dev,
-            int device_type, cec_logical_address_t* addr);
+            cec_device_type_t device_type, cec_logical_address_t* addr);
 
     /*
      * (*get_logical_address)() returns the logical address already allocated
@@ -232,7 +248,7 @@
      * Returns 0 on success or -errno on error.
      */
     int (*get_logical_address)(const struct hdmi_cec_device* dev,
-            int device_type, cec_logical_address_t* addr);
+            cec_device_type_t device_type, cec_logical_address_t* addr);
     /*
      * (*get_physical_address)() returns the CEC physical address. The
      * address is written to addr.
@@ -251,8 +267,7 @@
      *
      * Returns 0 on success or -errno on error.
      */
-    int (*send_message)(const struct hdmi_cec_device* dev,
-            const cec_message_t*);
+    int (*send_message)(const struct hdmi_cec_device* dev, const cec_message_t*);
 
     /*
      * (*register_event_callback)() registers a callback that HDMI-CEC HAL
diff --git a/include/hardware/tv_input.h b/include/hardware/tv_input.h
new file mode 100644
index 0000000..e04ad2f
--- /dev/null
+++ b/include/hardware/tv_input.h
@@ -0,0 +1,245 @@
+/*
+ * Copyright 2014 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_TV_INPUT_INTERFACE_H
+#define ANDROID_TV_INPUT_INTERFACE_H
+
+#include <stdint.h>
+#include <sys/cdefs.h>
+#include <sys/types.h>
+
+#include <hardware/hardware.h>
+
+__BEGIN_DECLS
+
+/*
+ * Module versioning information for the TV input hardware module, based on
+ * tv_input_module_t.common.module_api_version.
+ *
+ * Version History:
+ *
+ * TV_INPUT_MODULE_API_VERSION_0_1:
+ * Initial TV input hardware module API.
+ *
+ */
+
+#define TV_INPUT_MODULE_API_VERSION_0_1  HARDWARE_MODULE_API_VERSION(0, 1)
+
+#define TV_INPUT_DEVICE_API_VERSION_0_1  HARDWARE_DEVICE_API_VERSION(0, 1)
+
+/*
+ * The id of this module
+ */
+#define TV_INPUT_HARDWARE_MODULE_ID "tv_input"
+
+#define TV_INPUT_DEFAULT_DEVICE "default"
+
+/*****************************************************************************/
+
+/*
+ * 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 tv_input_module {
+    struct hw_module_t common;
+} tv_input_module_t;
+
+/*****************************************************************************/
+
+typedef enum tv_input_type {
+    /* HDMI */
+    TV_INPUT_TYPE_HDMI = 1,
+
+    /* Built-in tuners. */
+    TV_INPUT_TYPE_BUILT_IN_TUNER = 2,
+
+    /* Passthrough */
+    TV_INPUT_TYPE_PASSTHROUGH = 3,
+} tv_input_type_t;
+
+typedef struct tv_input_device_info {
+    /* Device ID */
+    int device_id;
+
+    /* Type of physical TV input. */
+    tv_input_type_t type;
+
+    /*
+     * TODO: A union of type specific information. For example, HDMI port
+     * identifier that HDMI hardware understands.
+     */
+
+    /* TODO: Add capability if necessary. */
+
+    /* TODO: Audio info */
+} tv_input_device_info_t;
+
+typedef enum {
+    /*
+     * Hardware notifies the framework that a device is available.
+     */
+    TV_INPUT_EVENT_DEVICE_AVAILABLE = 1,
+    /*
+     * Hardware notifies the framework that a device is unavailable.
+     */
+    TV_INPUT_EVENT_DEVICE_UNAVAILABLE = 2,
+    /*
+     * Stream configurations are changed. Client should regard all open streams
+     * at the specific device are closed, and should call
+     * get_stream_configurations() again, opening some of them if necessary.
+     */
+    TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED = 3,
+    /* TODO: Buffer notifications, etc. */
+} tv_input_event_type_t;
+
+typedef struct tv_input_event {
+    tv_input_event_type_t type;
+
+    union {
+        /*
+         * TV_INPUT_EVENT_DEVICE_AVAILABLE: all fields are relevant
+         * TV_INPUT_EVENT_DEVICE_UNAVAILABLE: only device_id is relevant
+         * TV_INPUT_EVENT_STREAM_CONFIGURATIONS_CHANGED: only device_id is
+         *    relevant
+         */
+        tv_input_device_info_t device_info;
+    };
+} tv_input_event_t;
+
+typedef struct tv_input_callback_ops {
+    /*
+     * event contains the type of the event and additional data if necessary.
+     * The event object is guaranteed to be valid only for the duration of the
+     * call.
+     *
+     * data is an object supplied at device initialization, opaque to the
+     * hardware.
+     */
+    void (*notify)(struct tv_input_device* dev,
+            tv_input_event_t* event, void* data);
+} tv_input_callback_ops_t;
+
+typedef enum {
+    TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE = 1,
+    /* TODO: TV_STREAM_TYPE_BUFFER_PRODUCER = 2, */
+} tv_stream_type_t;
+
+typedef struct tv_stream_config {
+    /*
+     * ID number of the stream. This value is used to identify the whole stream
+     * configuration.
+     */
+    int stream_id;
+
+    /* Type of the stream */
+    tv_stream_type_t type;
+
+    /* Max width/height of the stream. */
+    uint32_t max_video_width;
+    uint32_t max_video_height;
+} tv_stream_config_t;
+
+typedef struct tv_stream {
+    /* IN: ID in the stream configuration.  */
+    int stream_id;
+
+    /* OUT: Type of the stream (for convenience) */
+    tv_stream_type_t type;
+
+    /* OUT: Data associated with the stream for client's use */
+    union {
+        native_handle_t* sideband_stream_source_handle;
+        /* TODO: buffer_producer_stream_t buffer_producer; */
+    };
+} tv_stream_t;
+
+/*
+ * Every device data structure must begin with hw_device_t
+ * followed by module specific public methods and attributes.
+ */
+typedef struct tv_input_device {
+    struct hw_device_t common;
+
+    /*
+     * initialize:
+     *
+     * Provide callbacks to the device and start operation. At first, no device
+     * is available and after initialize() completes, currently available
+     * devices including static devices should notify via callback.
+     *
+     * Framework owns callbacks object.
+     *
+     * data is a framework-owned object which would be sent back to the
+     * framework for each callback notifications.
+     *
+     * Return 0 on success.
+     */
+    int (*initialize)(struct tv_input_device* dev,
+            const tv_input_callback_ops_t* callback, void* data);
+
+    /*
+     * get_stream_configurations:
+     *
+     * Get stream configurations for a specific device. An input device may have
+     * multiple configurations.
+     *
+     * The configs object is guaranteed to be valid only until the next call to
+     * get_stream_configurations() or STREAM_CONFIGURATIONS_CHANGED event.
+     *
+     * Return 0 on success.
+     */
+    int (*get_stream_configurations)(const struct tv_input_device* dev,
+            int device_id, int* num_configurations,
+            const tv_stream_config_t** configs);
+
+    /*
+     * open_stream:
+     *
+     * Open a stream with given stream ID. Caller owns stream object, and the
+     * populated data is only valid until the stream is closed.
+     *
+     * Return 0 on success; -EBUSY if the client should close other streams to
+     * open the stream; -EEXIST if the stream with the given ID is already open;
+     * -EINVAL if device_id and/or stream_id are invalid; other non-zero value
+     * denotes unknown error.
+     */
+    int (*open_stream)(struct tv_input_device* dev, int device_id,
+            tv_stream_t* stream);
+
+    /*
+     * close_stream:
+     *
+     * Close a stream to a device. data in tv_stream_t* object associated with
+     * the stream_id is obsolete once this call finishes.
+     *
+     * Return 0 on success; -ENOENT if the stream is not open; -EINVAL if
+     * device_id and/or stream_id are invalid.
+     */
+    int (*close_stream)(struct tv_input_device* dev, int device_id,
+            int stream_id);
+
+    /*
+     * TODO: Add more APIs such as buffer operations in case of buffer producer
+     * profile.
+     */
+
+    void* reserved[16];
+} tv_input_device_t;
+
+__END_DECLS
+
+#endif  // ANDROID_TV_INPUT_INTERFACE_H
diff --git a/modules/Android.mk b/modules/Android.mk
index f1a6c1c..5f1de32 100644
--- a/modules/Android.mk
+++ b/modules/Android.mk
@@ -1,4 +1,4 @@
 hardware_modules := gralloc hwcomposer audio nfc nfc-nci local_time \
 	power usbaudio audio_remote_submix camera consumerir sensors vibrator \
-	mcu
+	mcu tv_input
 include $(call all-named-subdir-makefiles,$(hardware_modules))
diff --git a/modules/sensors/multihal.cpp b/modules/sensors/multihal.cpp
index a145c37..36345f9 100644
--- a/modules/sensors/multihal.cpp
+++ b/modules/sensors/multihal.cpp
@@ -214,9 +214,11 @@
 }
 
 int sensors_poll_context_t::activate(int handle, int enabled) {
+    int retval = -EINVAL;
     ALOGV("activate");
     sensors_poll_device_t* v0 = this->get_v0_device_by_handle(handle);
-    int retval = v0->activate(v0, get_local_handle(handle), enabled);
+    if (v0)
+        retval = v0->activate(v0, get_local_handle(handle), enabled);
     ALOGV("retval %d", retval);
     return retval;
 }
@@ -576,7 +578,8 @@
         sensors_module_t *sensors_module = (sensors_module_t*) *it;
         struct hw_device_t* sub_hw_device;
         int sub_open_result = sensors_module->common.methods->open(*it, name, &sub_hw_device);
-        dev->addSubHwDevice(sub_hw_device);
+        if (!sub_open_result)
+            dev->addSubHwDevice(sub_hw_device);
     }
 
     // Prepare the output param and return
diff --git a/modules/tv_input/Android.mk b/modules/tv_input/Android.mk
new file mode 100644
index 0000000..e8aa7fc
--- /dev/null
+++ b/modules/tv_input/Android.mk
@@ -0,0 +1,24 @@
+# Copyright (C) 2014 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_RELATIVE_PATH := hw
+LOCAL_SHARED_LIBRARIES := libcutils liblog
+LOCAL_SRC_FILES := tv_input.cpp
+LOCAL_MODULE := tv_input.default
+LOCAL_MODULE_TAGS := optional
+include $(BUILD_SHARED_LIBRARY)
diff --git a/modules/tv_input/tv_input.cpp b/modules/tv_input/tv_input.cpp
new file mode 100644
index 0000000..bdb47ca
--- /dev/null
+++ b/modules/tv_input/tv_input.cpp
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2014 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 <fcntl.h>
+#include <errno.h>
+
+#include <cutils/log.h>
+#include <cutils/native_handle.h>
+
+#include <hardware/tv_input.h>
+
+/*****************************************************************************/
+
+typedef struct tv_input_private {
+    tv_input_device_t device;
+
+    // Callback related data
+    const tv_input_callback_ops_t* callback;
+    void* callback_data;
+} tv_input_private_t;
+
+static int tv_input_device_open(const struct hw_module_t* module,
+        const char* name, struct hw_device_t** device);
+
+static struct hw_module_methods_t tv_input_module_methods = {
+    open: tv_input_device_open
+};
+
+tv_input_module_t HAL_MODULE_INFO_SYM = {
+    common: {
+        tag: HARDWARE_MODULE_TAG,
+        version_major: 0,
+        version_minor: 1,
+        id: TV_INPUT_HARDWARE_MODULE_ID,
+        name: "Sample TV input module",
+        author: "The Android Open Source Project",
+        methods: &tv_input_module_methods,
+    }
+};
+
+/*****************************************************************************/
+
+static int tv_input_initialize(struct tv_input_device* dev,
+        const tv_input_callback_ops_t* callback, void* data)
+{
+    if (dev == NULL || callback == NULL) {
+        return -EINVAL;
+    }
+    tv_input_private_t* priv = (tv_input_private_t*)dev;
+    if (priv->callback != NULL) {
+        return -EEXIST;
+    }
+
+    priv->callback = callback;
+    priv->callback_data = data;
+
+    return 0;
+}
+
+static int tv_input_get_stream_configurations(
+        const struct tv_input_device*, int, int*, const tv_stream_config_t**)
+{
+    return -EINVAL;
+}
+
+static int tv_input_open_stream(struct tv_input_device*, int, tv_stream_t*)
+{
+    return -EINVAL;
+}
+
+static int tv_input_close_stream(struct tv_input_device*, int, int)
+{
+    return -EINVAL;
+}
+
+/*****************************************************************************/
+
+static int tv_input_device_close(struct hw_device_t *dev)
+{
+    tv_input_private_t* priv = (tv_input_private_t*)dev;
+    if (priv) {
+        free(priv);
+    }
+    return 0;
+}
+
+/*****************************************************************************/
+
+static int tv_input_device_open(const struct hw_module_t* module,
+        const char* name, struct hw_device_t** device)
+{
+    int status = -EINVAL;
+    if (!strcmp(name, TV_INPUT_DEFAULT_DEVICE)) {
+        tv_input_private_t* dev = (tv_input_private_t*)malloc(sizeof(*dev));
+
+        /* initialize our state here */
+        memset(dev, 0, sizeof(*dev));
+
+        /* initialize the procs */
+        dev->device.common.tag = HARDWARE_DEVICE_TAG;
+        dev->device.common.version = TV_INPUT_DEVICE_API_VERSION_0_1;
+        dev->device.common.module = const_cast<hw_module_t*>(module);
+        dev->device.common.close = tv_input_device_close;
+
+        dev->device.initialize = tv_input_initialize;
+        dev->device.get_stream_configurations =
+                tv_input_get_stream_configurations;
+        dev->device.open_stream = tv_input_open_stream;
+        dev->device.close_stream = tv_input_close_stream;
+
+        *device = &dev->device.common;
+        status = 0;
+    }
+    return status;
+}
diff --git a/tests/camera2/CameraMultiStreamTests.cpp b/tests/camera2/CameraMultiStreamTests.cpp
index de1cfd6..a78950c 100644
--- a/tests/camera2/CameraMultiStreamTests.cpp
+++ b/tests/camera2/CameraMultiStreamTests.cpp
@@ -35,7 +35,10 @@
 #define CAMERA_MULTI_STREAM_DEBUGGING  0
 #define CAMERA_FRAME_TIMEOUT    1000000000LL // nsecs (1 secs)
 #define PREVIEW_RENDERING_TIME_INTERVAL 200000 // in unit of us, 200ms
-#define TOLERANCE_MARGIN 0.01 // 1% tolerance margin for exposure sanity check.
+// 1% tolerance margin for exposure sanity check against metadata
+#define TOLERANCE_MARGIN_METADATA 0.01
+// 5% tolerance margin for exposure sanity check against capture times
+#define TOLERANCE_MARGIN_CAPTURE 0.05
 /* constants for display */
 #define DISPLAY_BUFFER_HEIGHT 1024
 #define DISPLAY_BUFFER_WIDTH 1024
@@ -399,10 +402,10 @@
             // TODO: Need revisit it to figure out an accurate margin.
             int64_t resultExposure = GetExposureValue(frameMetadata);
             int32_t resultSensitivity = GetSensitivity(frameMetadata);
-            EXPECT_LE(sensitivities[i] * (1.0 - TOLERANCE_MARGIN), resultSensitivity);
-            EXPECT_GE(sensitivities[i] * (1.0 + TOLERANCE_MARGIN), resultSensitivity);
-            EXPECT_LE(exposures[i] * (1.0 - TOLERANCE_MARGIN), resultExposure);
-            EXPECT_GE(exposures[i] * (1.0 + TOLERANCE_MARGIN), resultExposure);
+            EXPECT_LE(sensitivities[i] * (1.0 - TOLERANCE_MARGIN_METADATA), resultSensitivity);
+            EXPECT_GE(sensitivities[i] * (1.0 + TOLERANCE_MARGIN_METADATA), resultSensitivity);
+            EXPECT_LE(exposures[i] * (1.0 - TOLERANCE_MARGIN_METADATA), resultExposure);
+            EXPECT_GE(exposures[i] * (1.0 + TOLERANCE_MARGIN_METADATA), resultExposure);
 
             ASSERT_EQ(OK, listener->waitForFrame(waitLimit));
             captureBurstTimes.push_back(systemTime());
@@ -422,7 +425,7 @@
             if (i > 0) {
                 nsecs_t timeDelta =
                         captureBurstTimes[i] - captureBurstTimes[i-1];
-                EXPECT_GE(timeDelta, exposures[i]);
+                EXPECT_GE(timeDelta * ( 1 + TOLERANCE_MARGIN_CAPTURE), exposures[i]);
             }
         }
     }