hikey: Add hardware/ti/wpan code to hikey device dir

The bluetooth userspace for hikey is related but newer
then the hardware/ti/wpan project in AOSP. Since we have
some hikey specific modifications, import the hikey version
into the device/linaro/hikey directory and tweak the build
to include it.

Code included from:
https://github.com/96boards/android_hardware_ti_wpan.git marshmallow-dev

Change-Id: I6246be43cb287bfd975b099a0e6508046e6d3add
Signed-off-by: John Stultz <john.stultz@linaro.org>
diff --git a/device.mk b/device.mk
index a3cc8c2..7e6e8ac 100644
--- a/device.mk
+++ b/device.mk
@@ -74,7 +74,7 @@
                        vendor/linaro/hikey/mali/32bit/libGLES_mali.so:system/lib/egl/libGLES_mali.so
 
 # Include BT modules
-$(call inherit-product-if-exists, hardware/ti/wpan/ti-wpan-products.mk)
+$(call inherit-product-if-exists, devices/linaro/hikey/wpan/ti-wpan-products.mk)
 
 # Build boot_fat partition image
 $(call inherit-product-if-exists, device/linaro/hikey/boot_fat.mk)
diff --git a/wpan/Android.mk b/wpan/Android.mk
new file mode 100644
index 0000000..edb7b6f
--- /dev/null
+++ b/wpan/Android.mk
@@ -0,0 +1 @@
+include $(call first-makefiles-under,$(call my-dir))
diff --git a/wpan/README b/wpan/README
new file mode 100644
index 0000000..3b45293
--- /dev/null
+++ b/wpan/README
@@ -0,0 +1,18 @@
+wpan.git contains the Bluetooth, FM code by Texas Instruments for
+WiLink connectivity chipsets on OMAP Platforms
+
+Android.mk - Root Makefile calling all subdir makefiles
+bt_sco_app - Command line application to send PCM configuruations to WL12xx chip(optional)
+and connect to BT headset via SCO.
+bt_voice_call_set - Derived from bt_sco_app for Modem purposes.
+fmradio - Stack and Command line application for FM on WiLink chipsets
+fmradio_omap3 - Derived from fmradio (older version) on OMAP3 platforms
+README - This file
+ti_st -
+	kfmapp/ - V4L2 FM application to be used with V4L2 drivers for FM on WL.
+	uim/ - User-Space Initilization Manager for TI Shared Transport Drivers (to be
+	used with kernel version 2.6.32 and below drivers)
+	uim-rfkill/ - Derived from uim/ to be used with K32 and above TI ST drivers.
+	uim-sysfs/ - User-Space Init Manager for TI Shared Transport Drivers (per kernel.org)
+tools -
+        BluetoothSCOApp/ - BT SCO UI App for connect to BT headset via SCO
diff --git a/wpan/bluedroid_wilink/Android.mk b/wpan/bluedroid_wilink/Android.mk
new file mode 100644
index 0000000..6b57838
--- /dev/null
+++ b/wpan/bluedroid_wilink/Android.mk
@@ -0,0 +1,39 @@
+#
+# Copyright 2001-2012 Texas Instruments, Inc. - http://www.ti.com/
+#
+# 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.
+#
+
+ifeq (hikey, $(TARGET_PRODUCT))
+LOCAL_PATH := $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_C_INCLUDES := system/bt/hci/include hardware/libhardware/include
+
+LOCAL_CFLAGS := -g -c -W -Wall -O2 -D_POSIX_SOURCE
+
+LOCAL_SRC_FILES := libbt-vendor-ti.c
+
+LOCAL_SHARED_LIBRARIES := \
+	libnativehelper \
+	libcutils \
+	libutils \
+	liblog
+
+LOCAL_PRELINK_MODULE := false
+LOCAL_MODULE := libbt-vendor
+LOCAL_MODULE_TAGS := optional
+#LOCAL_MODULE_PATH := $(TARGET_OUT_VENDOR_SHARED_LIBRARIES)
+
+include $(BUILD_SHARED_LIBRARY)
+endif
diff --git a/wpan/bluedroid_wilink/libbt-vendor-ti.c b/wpan/bluedroid_wilink/libbt-vendor-ti.c
new file mode 100644
index 0000000..e05c154
--- /dev/null
+++ b/wpan/bluedroid_wilink/libbt-vendor-ti.c
@@ -0,0 +1,111 @@
+/*
+ * Copyright 2001-2012 Texas Instruments, Inc. - http://www.ti.com/
+ *
+ * Bluetooth Vendor Library for TI's WiLink Chipsets
+ *
+ * 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 "bt_vendor"
+
+#include <stdio.h>
+#include <dlfcn.h>
+#include <utils/Log.h>
+#include <pthread.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <bt_vendor_lib.h>
+#include <bt_hci_bdroid.h>
+#include <hardware/bluetooth.h>
+
+bt_vendor_callbacks_t *bt_vendor_cbacks = NULL;
+unsigned int hci_tty_fd = -1;
+void hw_config_cback(HC_BT_HDR *p_evt_buf);
+
+/*******************************************************************************
+ *
+ * Function         hw_config_cback
+ *
+ * Description      Callback function for controller configuration
+ *
+ * Returns          None
+ *
+ * *******************************************************************************/
+void hw_config_cback(HC_BT_HDR *p_evt_buf)
+{
+    ALOGV("hw_config_cback");
+}
+
+int ti_init(const bt_vendor_callbacks_t* p_cb, unsigned char *local_bdaddr) {
+    ALOGV("vendor Init");
+
+    if (p_cb == NULL)
+    {
+        ALOGE("init failed with no user callbacks!");
+        return BT_STATUS_FAIL;
+    }
+
+    bt_vendor_cbacks = (bt_vendor_callbacks_t *) p_cb;
+
+    return 0;
+}
+void ti_cleanup(void) {
+    ALOGV("vendor cleanup");
+
+    bt_vendor_cbacks = NULL;
+}
+int ti_op(bt_vendor_opcode_t opcode, void **param) {
+    int fd;
+    int *fd_array = (int (*)[]) param;
+
+    ALOGV("vendor op - %d", opcode);
+    switch(opcode)
+    {
+        case BT_VND_OP_USERIAL_OPEN:
+            fd = open("/dev/hci_tty", O_RDWR);
+            if (fd < 0) {
+                ALOGE(" Can't open hci_tty");
+                return -1;
+            }
+            fd_array[CH_CMD] = fd;
+            hci_tty_fd = fd; /* for userial_close op */
+            return 1;        /* CMD/EVT/ACL on same fd */
+        case BT_VND_OP_USERIAL_CLOSE:
+            close(hci_tty_fd);
+            return 0;
+        /* Since new stack expects fwcfg_cb we are returning SUCCESS here
+         * in actual, firmware download is already happened when /dev/hci_tty
+         * opened.
+         */
+        case BT_VND_OP_FW_CFG:
+            bt_vendor_cbacks->fwcfg_cb(BT_VND_OP_RESULT_SUCCESS);
+            return 0;
+        case BT_VND_OP_EPILOG:
+            bt_vendor_cbacks->epilog_cb(BT_VND_OP_RESULT_SUCCESS);
+            break;
+        default:
+            break;
+    }
+
+    return 0;
+}
+const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE  = {
+    .init = ti_init,
+    .op = ti_op,
+    .cleanup = ti_cleanup,
+};
+
+int main()
+{
+    return 0;
+}
diff --git a/wpan/ti-wpan-products.mk b/wpan/ti-wpan-products.mk
new file mode 100644
index 0000000..ffee1f3
--- /dev/null
+++ b/wpan/ti-wpan-products.mk
@@ -0,0 +1,5 @@
+# This file lists the firmware, software that are specific to
+# WiLink connectivity chip on OMAPx platforms.
+
+PRODUCT_PACKAGES += uim \
+        libbt-vendor
diff --git a/wpan/uim/Android.mk b/wpan/uim/Android.mk
new file mode 100644
index 0000000..2beec72
--- /dev/null
+++ b/wpan/uim/Android.mk
@@ -0,0 +1,15 @@
+#Android makefile for uim
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+
+LOCAL_SRC_FILES:= uim.c
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/
+
+LOCAL_MODULE_TAGS := eng
+
+LOCAL_MODULE := uim
+
+include $(BUILD_EXECUTABLE)
+
diff --git a/wpan/uim/Makefile b/wpan/uim/Makefile
new file mode 100644
index 0000000..2790e79
--- /dev/null
+++ b/wpan/uim/Makefile
@@ -0,0 +1,46 @@
+MAKEFLAGS += --no-print-directory
+
+PREFIX ?= /usr
+SBINDIR ?= $(PREFIX)/sbin
+MANDIR ?= $(PREFIX)/share/man
+
+MKDIR ?= mkdir -p
+INSTALL ?= install
+CC ?= "gcc"
+
+CFLAGS ?= -O2 -g
+CFLAGS += -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration
+
+OBJS = uim.o
+ALL = uim
+
+ifeq ($(V),1)
+Q=
+NQ=true
+else
+Q=@
+NQ=echo
+endif
+
+all: $(ALL)
+
+VERSION_OBJS := $(filter-out version.o, $(OBJS))
+
+%.o: %.c uim.h
+	@$(NQ) ' CC  ' $@
+	$(Q)$(CC) $(CFLAGS) -c -o $@ $<
+
+uim:	$(OBJS)
+	@$(NQ) ' CC  ' uim
+	$(Q)$(CC) $(LDFLAGS) $(OBJS) $(LIBS) -o uim
+
+check:
+	$(Q)$(MAKE) all CC="REAL_CC=$(CC) CHECK=\"sparse -Wall\" cgcc"
+
+install: uim
+	@$(NQ) ' INST uim'
+	$(Q)$(MKDIR) $(DESTDIR)$(SBINDIR)
+	$(Q)$(INSTALL) -m 755 -t $(DESTDIR)$(SBINDIR) uim
+
+clean:
+	$(Q)rm -f uim *.o *~ 
diff --git a/wpan/uim/uim.c b/wpan/uim/uim.c
new file mode 100644
index 0000000..a9dde00
--- /dev/null
+++ b/wpan/uim/uim.c
@@ -0,0 +1,545 @@
+/*
+ *  User Mode Init manager - For TI shared transport
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program;if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <string.h>
+#include <signal.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <poll.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <sys/utsname.h>
+
+#include <unistd.h>
+#include <time.h>
+
+#include "uim.h"
+
+/* Maintains the exit state of UIM*/
+static int exiting;
+static int line_discipline;
+static int dev_fd;
+
+/* BD address as string and a pointer to array of hex bytes */
+char uim_bd_address[BD_ADDR_LEN];
+bdaddr_t *bd_addr;
+
+/*****************************************************************************/
+#ifdef UIM_DEBUG
+/*  Function to Read the firmware version
+ *  module into the system. Currently used for
+ *  debugging purpose, whenever the baud rate is changed
+ */
+void read_firmware_version(int dev_fd)
+{
+	int index = 0;
+	char resp_buffer[20] = { 0 };
+	unsigned char buffer[] = { 0x01, 0x01, 0x10, 0x00 };
+
+	UIM_START_FUNC();
+	UIM_VER(" wrote %d bytes", (int)write(dev_fd, buffer, 4));
+	UIM_VER(" reading %d bytes", (int)read(dev_fd, resp_buffer, 15));
+
+	for (index = 0; index < 15; index++)
+		UIM_VER(" %x ", resp_buffer[index]);
+
+	printf("\n");
+}
+#endif
+
+/*****************************************************************************/
+/* Function to read the HCI event from the given file descriptor
+ *
+ * This will parse the response received and returns error
+ * if the required response is not received
+ */
+int read_hci_event(int fd, unsigned char *buf, int size)
+{
+	int remain, rd;
+	int count = 0;
+	int reading = 1;
+	int rd_retry_count = 0;
+	struct timespec tm = {0, 50*1000*1000};
+
+	UIM_START_FUNC();
+
+	UIM_VER(" read_hci_event");
+	if (size <= 0)
+		return -1;
+
+	/* The first byte identifies the packet type. For HCI event packets, it
+	 * should be 0x04, so we read until we get to the 0x04. */
+	while (reading) {
+		rd = read(fd, buf, 1);
+		if (rd <= 0 && rd_retry_count++ < 4) {
+			nanosleep(&tm, NULL);
+			continue;
+		} else if (rd_retry_count >= 4) {
+			return -1;
+		}
+
+		if (buf[0] == RESP_PREFIX) {
+			break;
+		}
+	}
+	count++;
+
+	/* The next two bytes are the event code and parameter total length. */
+	while (count < 3) {
+		rd = read(fd, buf + count, 3 - count);
+		if (rd <= 0)
+			return -1;
+		count += rd;
+	}
+
+	/* Now we read the parameters. */
+	if (buf[2] < (size - 3))
+		remain = buf[2];
+	else
+		remain = size - 3;
+
+	while ((count - 3) < remain) {
+		rd = read(fd, buf + count, remain - (count - 3));
+		if (rd <= 0)
+			return -1;
+		count += rd;
+	}
+
+	return count;
+}
+
+/* Function to read the Command complete event
+ *
+ * This will read the response for the change speed
+ * command that was sent to configure the UART speed
+ * with the custom baud rate
+ */
+static int read_command_complete(int fd, unsigned short opcode)
+{
+	command_complete_t resp;
+
+	UIM_START_FUNC();
+
+	UIM_VER(" Command complete started");
+	if (read_hci_event(fd, (unsigned char *)&resp, sizeof(resp)) < 0) {
+		UIM_ERR("Invalid response");
+		return -1;
+	}
+
+	/* Response should be an event packet */
+	if (resp.uart_prefix != HCI_EVENT_PKT) {
+		UIM_ERR	("Error in response: not an event packet, 0x%02x!",
+			 resp.uart_prefix);
+		return -1;
+	}
+
+	/* Response should be a command complete event */
+	if (resp.hci_hdr.evt != EVT_CMD_COMPLETE) {
+		/* event must be event-complete */
+		UIM_ERR("Error in response: not a cmd-complete event,0x%02x!",
+			 resp.hci_hdr.evt);
+		return -1;
+	}
+
+	if (resp.hci_hdr.plen < 4) {
+		/* plen >= 4 for EVT_CMD_COMPLETE */
+		UIM_ERR("Error in response: plen is not >= 4, but 0x%02x!",
+				resp.hci_hdr.plen);
+		return -1;
+	}
+
+	if (resp.cmd_complete.opcode != (unsigned short)opcode) {
+		UIM_ERR("Error in response: opcode is 0x%04x, not 0x%04x!",
+				resp.cmd_complete.opcode, opcode);
+		return -1;
+	}
+
+	UIM_DBG("Command complete done");
+	return resp.status == 0 ? 0 : -1;
+}
+
+/* Function to set the default baud rate
+ *
+ * The default baud rate of 115200 is set to the UART from the host side
+ * by making a call to this function.This function is also called before
+ * making a call to set the custom baud rate
+ */
+static int set_baud_rate(int dev_fd)
+{
+	UIM_START_FUNC();
+	struct termios ti;
+
+	tcflush(dev_fd, TCIOFLUSH);
+
+	/* Get the attributes of UART */
+	if (tcgetattr(dev_fd, &ti) < 0) {
+		UIM_ERR(" Can't get port settings");
+		return -1;
+	}
+
+	/* Change the UART attributes before
+	 * setting the default baud rate*/
+	cfmakeraw(&ti);
+
+	ti.c_cflag |= 1;
+	ti.c_cflag |= CRTSCTS;
+
+	/* Set the attributes of UART after making
+	 * the above changes
+	 */
+	tcsetattr(dev_fd, TCSANOW, &ti);
+
+	/* Set the actual default baud rate */
+	cfsetospeed(&ti, B115200);
+	cfsetispeed(&ti, B115200);
+	tcsetattr(dev_fd, TCSANOW, &ti);
+
+	tcflush(dev_fd, TCIOFLUSH);
+	UIM_DBG("set_baud_rate() done");
+
+	return 0;
+}
+
+
+/* Function to set the UART custom baud rate.
+ *
+ * The UART baud rate has already been
+ * set to default value 115200 before calling this function.
+ * The baud rate is then changed to custom baud rate by this function*/
+static int set_custom_baud_rate(int dev_fd, int baud_rate, int flow_ctrl)
+{
+	UIM_START_FUNC();
+
+	struct termios ti;
+	struct termios2 ti2;
+
+	tcflush(dev_fd, TCIOFLUSH);
+	/* Get the attributes of UART */
+	if (tcgetattr(dev_fd, &ti) < 0) {
+		UIM_ERR(" Can't get port settings");
+		return -1;
+	}
+
+	/*Set the UART flow control */
+	if (flow_ctrl)
+		ti.c_cflag |= CRTSCTS;
+	else
+		ti.c_cflag &= ~CRTSCTS;
+
+	/*
+	 * Set the parameters associated with the UART
+	 * The change will occur immediately by using TCSANOW
+	 */
+	if (tcsetattr(dev_fd, TCSANOW, &ti) < 0) {
+		UIM_ERR(" Can't set port settings");
+		return -1;
+	}
+
+	tcflush(dev_fd, TCIOFLUSH);
+
+	/*Set the actual baud rate */
+	ioctl(dev_fd, TCGETS2, &ti2);
+	ti2.c_cflag &= ~CBAUD;
+	ti2.c_cflag |= BOTHER;
+	ti2.c_ospeed = baud_rate;
+	ioctl(dev_fd, TCSETS2, &ti2);
+
+	return 0;
+}
+
+/* Function to configure the UART
+ * on receiving a notification from the ST KIM driver to install the line
+ * discipline, this function does UART configuration necessary for the STK
+ */
+int st_uart_config(unsigned char install)
+{
+	int ldisc, len, fd, flow_ctrl;
+	unsigned char buf[UART_DEV_NAME_LEN];
+	uim_speed_change_cmd cmd;
+	char uart_dev_name[UART_DEV_NAME_LEN];
+	unsigned int cust_baud_rate;
+
+	uim_bdaddr_change_cmd addr_cmd;
+
+	UIM_START_FUNC();
+
+	if (install == '1') {
+		memset(buf, 0, UART_DEV_NAME_LEN);
+		fd = open(DEV_NAME_SYSFS, O_RDONLY);
+		if (fd < 0) {
+			UIM_ERR("Can't open %s", DEV_NAME_SYSFS);
+			return -1;
+		}
+		len = read(fd, buf, UART_DEV_NAME_LEN);
+		if (len < 0) {
+			UIM_ERR("read err (%s)", strerror(errno));
+			close(fd);
+			return len;
+		}
+		sscanf((const char*)buf, "%s", uart_dev_name);
+		close(fd);
+
+		memset(buf, 0, UART_DEV_NAME_LEN);
+		fd = open(BAUD_RATE_SYSFS, O_RDONLY);
+		if (fd < 0) {
+			UIM_ERR("Can't open %s", BAUD_RATE_SYSFS);
+			return -1;
+		}
+		len = read(fd, buf, UART_DEV_NAME_LEN);
+		if (len < 0) {
+			UIM_ERR("read err (%s)", strerror(errno));
+			close(fd);
+			return len;
+		}
+		close(fd);
+		sscanf((const char*)buf, "%d", &cust_baud_rate);
+
+		memset(buf, 0, UART_DEV_NAME_LEN);
+		fd = open(FLOW_CTRL_SYSFS, O_RDONLY);
+		if (fd < 0) {
+			UIM_ERR("Can't open %s", FLOW_CTRL_SYSFS);
+			close(fd);
+			return -1;
+		}
+		len = read(fd, buf, UART_DEV_NAME_LEN);
+		if (len < 0) {
+			UIM_ERR("read err (%s)", strerror(errno));
+			close(fd);
+			return len;
+		}
+		close(fd);
+		sscanf((const char*)buf, "%d", &flow_ctrl);
+
+		UIM_VER(" signal received, opening %s", uart_dev_name);
+
+		dev_fd = open(uart_dev_name, O_RDWR);
+		if (dev_fd < 0) {
+			UIM_ERR("Can't open %s", uart_dev_name);
+			return -1;
+		}
+
+		/*
+		 * Set only the default baud rate.
+		 * This will set the baud rate to default 115200
+		 */
+		if (set_baud_rate(dev_fd) < 0) {
+			UIM_ERR("set_baudrate() failed");
+			close(dev_fd);
+			return -1;
+		}
+
+		fcntl(dev_fd, F_SETFL,fcntl(dev_fd, F_GETFL) | O_NONBLOCK);
+		/* Set only the custom baud rate */
+		if (cust_baud_rate != 115200) {
+
+			UIM_VER("Setting speed to %d", cust_baud_rate);
+			/* Forming the packet for Change speed command */
+			cmd.uart_prefix = HCI_COMMAND_PKT;
+			cmd.hci_hdr.opcode = HCI_HDR_OPCODE;
+			cmd.hci_hdr.plen = sizeof(unsigned int);
+			cmd.speed = cust_baud_rate;
+
+			/* Writing the change speed command to the UART
+			 * This will change the UART speed at the controller
+			 * side
+			 */
+			len = write(dev_fd, &cmd, sizeof(cmd));
+			if (len < 0) {
+				UIM_ERR("Failed to write speed-set command");
+				close(dev_fd);
+				return -1;
+			}
+
+			/* Read the response for the Change speed command */
+			if (read_command_complete(dev_fd, HCI_HDR_OPCODE) < 0) {
+				close(dev_fd);
+				return -1;
+			}
+
+			UIM_VER("Speed changing to %d, %d", cust_baud_rate, flow_ctrl);
+			/* Set the actual custom baud rate at the host side */
+			if (set_custom_baud_rate(dev_fd, cust_baud_rate, flow_ctrl) < 0) {
+				UIM_ERR("set_custom_baud_rate() failed");
+				close(dev_fd);
+				return -1;
+			}
+
+			/* Set the uim BD address */
+			if (uim_bd_address[0] != 0) {
+
+				memset(&addr_cmd, 0, sizeof(addr_cmd));
+				/* Forming the packet for change BD address command*/
+				addr_cmd.uart_prefix = HCI_COMMAND_PKT;
+				addr_cmd.hci_hdr.opcode = WRITE_BD_ADDR_OPCODE;
+				addr_cmd.hci_hdr.plen = sizeof(bdaddr_t);
+				memcpy(&addr_cmd.addr, bd_addr, sizeof(bdaddr_t));
+
+				/* Writing the change BD address command to the UART
+				 * This will change the change BD address  at the controller
+				 * side
+				 */
+				len = write(dev_fd, &addr_cmd, sizeof(addr_cmd));
+				if (len < 0) {
+					UIM_ERR("Failed to write BD address command");
+					close(dev_fd);
+					return -1;
+				}
+
+				/* Read the response for the change BD address command */
+				if (read_command_complete(dev_fd, WRITE_BD_ADDR_OPCODE) < 0) {
+					close(dev_fd);
+					return -1;
+				}
+				UIM_VER("BD address changed to %s", uim_bd_address);
+			}
+#ifdef UIM_DEBUG
+			read_firmware_version(dev_fd);
+#endif
+		}
+
+		/* After the UART speed has been changed, the IOCTL is
+		 * is called to set the line discipline to N_TI_WL
+		 */
+		ldisc = N_TI_WL;
+		if (ioctl(dev_fd, TIOCSETD, &ldisc) < 0) {
+			UIM_ERR(" Can't set line discipline");
+			close(dev_fd);
+			return -1;
+		}
+		UIM_DBG("Installed N_TI_WL Line displine");
+	}
+	else {
+		UIM_DBG("Un-Installed N_TI_WL Line displine");
+		/* UNINSTALL_N_TI_WL - When the Signal is received from KIM */
+		/* closing UART fd */
+		close(dev_fd);
+	}
+	return 0;
+}
+
+/* Function to convert the BD address from ascii to hex value */
+bdaddr_t *strtoba(const char *str)
+{
+        const char *ptr = str;
+        int i;
+
+        uint8_t *ba = malloc(sizeof(bdaddr_t));
+        if (!ba)
+                return NULL;
+
+        for (i = 0; i < 6; i++) {
+                ba[i] = (uint8_t) strtol(ptr, NULL, 16);
+                if (i != 5 && !(ptr = strchr(ptr, ':')))
+                        ptr = ":00:00:00:00:00";
+                ptr++;
+        }
+
+        return (bdaddr_t *) ba;
+}
+
+/*****************************************************************************/
+int main(int argc, char *argv[])
+{
+	int st_fd, err,trials;
+	unsigned char install;
+	struct pollfd 	p;
+
+	UIM_START_FUNC();
+	err = 0;
+	trials = 5;
+
+	/* Parse the user input */
+	if ((argc > 2)) {
+		UIM_ERR("Invalid arguements");
+		UIM_ERR("Usage: uim <bd address>");
+		return -1;
+	}
+	if (argc == 2) {
+		if (strlen(argv[2]) != BD_ADDR_LEN) {
+			UIM_ERR("Usage: uim XX:XX:XX:XX:XX:XX");
+			return -1;
+		}
+		/* BD address passed as string in xx:xx:xx:xx:xx:xx format */
+		strncpy(uim_bd_address, argv[2], sizeof(uim_bd_address));
+		bd_addr = strtoba(uim_bd_address);
+	}
+
+	line_discipline = N_TI_WL;
+
+	/* sysfs entry may get populated after service is started so we retry if it fails*/
+	while (trials > 0) {
+		st_fd = open(INSTALL_SYSFS_ENTRY, O_RDONLY);
+		if(st_fd > 0)
+			break;
+		usleep(500000);
+		--trials;
+		}
+	if (st_fd < 0) {
+		UIM_DBG("unable to open %s(%s)", INSTALL_SYSFS_ENTRY, strerror(errno));
+		return -1;
+	}
+
+RE_POLL:
+	/* read to start proper poll */
+	err = read(st_fd, &install, 1);
+	/* special case where bluetoothd starts before the UIM, and UIM
+	 * needs to turn on bluetooth because of that.
+	 */
+	if ((err > 0) && install == '1') {
+		UIM_DBG("install set previously...");
+		st_uart_config(install);
+	}
+
+	UIM_DBG("begin polling...");
+
+	memset(&p, 0, sizeof(p));
+	p.fd = st_fd;
+	p.events = POLLERR | POLLPRI;
+
+	while (!exiting) {
+		p.revents = 0;
+		err = poll(&p, 1, -1);
+		UIM_DBG("poll broke due to event %d(PRI:%d/ERR:%d)\n", p.revents, POLLPRI, POLLERR);
+		if (err < 0 && errno == EINTR)
+			continue;
+		if (err)
+			break;
+	}
+
+	close(st_fd);
+	st_fd = open(INSTALL_SYSFS_ENTRY, O_RDONLY);
+	if (st_fd < 0) {
+		UIM_DBG("unable to open %s (%s)", INSTALL_SYSFS_ENTRY, strerror(errno));
+		return -1;
+	}
+
+	if (!exiting)
+	{
+		err = read(st_fd, &install, 1);
+		UIM_DBG("read %c from install \n", install);
+		if (err > 0)
+			st_uart_config(install);
+		goto RE_POLL;
+	}
+
+	close(st_fd);
+	return 0;
+}
diff --git a/wpan/uim/uim.h b/wpan/uim/uim.h
new file mode 100644
index 0000000..c21abda
--- /dev/null
+++ b/wpan/uim/uim.h
@@ -0,0 +1,144 @@
+/*
+ *  User Mode Init manager - For shared transport
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program;if not, write to the Free Software
+ *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#ifndef UIM_H
+#define UIM_H
+
+/* the line discipline ideally should be coming
+ * from tty.h
+ */
+#define N_TI_WL 22
+
+/* Paramaters to set the baud rate*/
+#define  FLOW_CTL	0x0001
+#define  BOTHER		0x00001000
+#define  ARM_NCCS	19
+
+#ifndef TCGETS2
+#define TCGETS2      _IOR('T',0x2A, struct termios2)
+#endif
+#ifndef TCSETS2
+#define TCSETS2      _IOW('T',0x2B, struct termios2)
+#endif
+
+/*HCI Command and Event information*/
+#define HCI_HDR_OPCODE		0xff36
+#define WRITE_BD_ADDR_OPCODE    0xFC06
+#define RESP_PREFIX		0x04
+#define MAX_TRY			10
+
+/* HCI Packet types */
+#define HCI_COMMAND_PKT		0x01
+#define HCI_EVENT_PKT		0x04
+
+/* HCI command macros*/
+#define HCI_EVENT_HDR_SIZE      2
+#define HCI_COMMAND_HDR_SIZE	3
+#define HCI_COMMAND_HDR_SIZE    3
+
+/* HCI event macros*/
+#define EVT_CMD_COMPLETE_SIZE	3
+#define EVT_CMD_STATUS_SIZE	4
+#define EVT_CMD_COMPLETE	0x0E
+#define EVT_CMD_STATUS		0x0F
+
+/* use it for string lengths and buffers */
+#define UART_DEV_NAME_LEN	32
+/* BD address length in format xx:xx:xx:xx:xx:xx */
+#define BD_ADDR_LEN		17
+
+/* the sysfs entries with device configuration set by
+ * shared transport driver
+ */
+#define INSTALL_SYSFS_ENTRY "/sys/devices/platform/kim/install"
+#define DEV_NAME_SYSFS "/sys/devices/platform/kim/dev_name"
+#define BAUD_RATE_SYSFS "/sys/devices/platform/kim/baud_rate"
+#define FLOW_CTRL_SYSFS "/sys/devices/platform/kim/flow_cntrl"
+
+
+#define VERBOSE
+/*Debug logs*/
+#define UIM_ERR(fmt, arg...)  printf("uim:"fmt"\n" , ##arg)
+#if defined(UIM_DEBUG)		/* limited debug messages */
+#define UIM_START_FUNC()      printf("uim: Inside %s", __FUNCTION__)
+#define UIM_DBG(fmt, arg...)  printf("uim:"fmt"\n" , ## arg)
+#define UIM_VER(fmt, arg...)
+#elif defined(VERBOSE)		/* very verbose */
+#define UIM_START_FUNC()      printf("uim:@ %s\n", __FUNCTION__)
+#define UIM_DBG(fmt, arg...)  printf("uim:"fmt"\n" , ## arg)
+#define UIM_VER(fmt, arg...)  printf("uim:"fmt"\n" , ## arg)
+#else /* error msgs only */
+#define UIM_START_FUNC()
+#define UIM_DBG(fmt, arg...)
+#define UIM_VER(fmt, arg...)
+#endif
+
+/* HCI command header*/
+typedef struct {
+        uint16_t        opcode;         /* OCF & OGF */
+        uint8_t         plen;
+} __attribute__ ((packed))      hci_command_hdr;
+
+/* HCI event header*/
+typedef struct {
+        uint8_t         evt;
+        uint8_t         plen;
+} __attribute__ ((packed))      hci_event_hdr;
+
+/* HCI command complete event*/
+typedef struct {
+        uint8_t         ncmd;
+        uint16_t        opcode;
+} __attribute__ ((packed)) evt_cmd_complete;
+
+/* HCI event status*/
+typedef struct {
+        uint8_t         status;
+        uint8_t         ncmd;
+        uint16_t        opcode;
+} __attribute__ ((packed)) evt_cmd_status;
+
+/* HCI Event structure to set the cusrom baud rate*/
+typedef struct {
+	uint8_t uart_prefix;
+	hci_event_hdr hci_hdr;
+	evt_cmd_complete cmd_complete;
+	uint8_t status;
+	uint8_t data[16];
+} __attribute__ ((packed)) command_complete_t;
+
+/* HCI Command structure to set the cusrom baud rate*/
+typedef struct {
+	uint8_t uart_prefix;
+	hci_command_hdr hci_hdr;
+	uint32_t speed;
+} __attribute__ ((packed)) uim_speed_change_cmd;
+
+/* BD address structure to set the uim BD address*/
+typedef struct {
+        unsigned char b[6];
+} __attribute__((packed)) bdaddr_t;
+
+/* HCI Command structure to set the uim BD address*/
+typedef struct {
+        uint8_t uart_prefix;
+        hci_command_hdr hci_hdr;
+        bdaddr_t addr;
+} __attribute__ ((packed)) uim_bdaddr_change_cmd;\
+
+#endif /* UIM_H */