Tizen 2.1 base
authorJinkun Jang <jinkun.jang@samsung.com>
Tue, 12 Mar 2013 16:45:23 +0000 (01:45 +0900)
committerJinkun Jang <jinkun.jang@samsung.com>
Tue, 12 Mar 2013 16:45:23 +0000 (01:45 +0900)
packaging/uim.changes [new file with mode: 0644]
packaging/uim.spec [new file with mode: 0644]
uim.c
uim.h

diff --git a/packaging/uim.changes b/packaging/uim.changes
new file mode 100644 (file)
index 0000000..a6c2d10
--- /dev/null
@@ -0,0 +1,14 @@
+* Wed Sept 19 2012 Zheng, wu <wu.zheng@intel.com>
+- fix license in spec file
+
+* Tue Sept 11 2012 Zheng, wu <wu.zheng@intel.com>
+- Fix the bluetooth can't pair with the other device issue.
+
+* Fri Aug 31 2012 Patrick McCarty <patrick.mccarty@linux.intel.com> dbd7662
+- Repackage the sysvinit script and symlink
+
+* Tue Aug 07 2012 Patrick McCarty <patrick.mccarty@linux.intel.com> 3dab3d8
+- systemd: move the module loading to a new service file
+
+* Mon Aug 06 2012 Patrick McCarty <patrick.mccarty@linux.intel.com> 925f272
+- Add/install a systemd service file
diff --git a/packaging/uim.spec b/packaging/uim.spec
new file mode 100644 (file)
index 0000000..a6f9e65
--- /dev/null
@@ -0,0 +1,24 @@
+Name:           uim
+Version:       0.1
+Release:       1
+License:       GPLv2+
+Summary:       User Mode Init manager for bluetooth device in pr3      
+Group:         Communications/Bluetooth
+Source:                %{name}-%{version}.tar.gz
+BuildRequires: pkgconfig(bluez)
+
+%description
+User Mode Init manager for tiwl1283
+
+%prep
+%setup -q
+
+%build
+gcc -o uim uim.c
+
+%install
+mkdir -p %{buildroot}/bin/
+cp -f uim %{buildroot}/bin/
+
+%files
+%attr(0755,-,-) /bin/uim
diff --git a/uim.c b/uim.c
index c4e690b..60e3400 100644 (file)
--- a/uim.c
+++ b/uim.c
@@ -1,5 +1,5 @@
 /*
- *  User Mode Init manager - For shared transport
+ *  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
  *  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 <stdlib.h>
 #include <string.h>
 #include <signal.h>
-#include <time.h>
-#include <sys/time.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 <sys/types.h>
+#include "uim.h"
 #ifdef ANDROID
 #include <private/android_filesystem_config.h>
+#include <cutils/log.h>
 #endif
 
-#include "uim.h"
-
-/* Maintains the exit state of UIM*/
-static int exiting;
-#define UIM_DEBUG
 
 /* 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+1];
+char uim_bd_address[17];
 bdaddr_t *bd_addr;
 
+/* File descriptor for the UART device*/
+int dev_fd;
+
+static inline void cleanup(int failed)
+{
+       /* for future use */
+       (void)failed;
+
+       if (dev_fd == -1)
+               return;
+
+       UIM_DBG("%s", __func__);
+
+       close(dev_fd);
+       dev_fd = -1;
+       /* unused failed for future reference */
+}
+
 /*****************************************************************************/
 #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)
+void read_firmware_version()
 {
        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));
+       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
+#endif /* UIM_DEBUG */
+
+/*****************************************************************************/
+#ifdef ANDROID                 /* library for android to do insmod/rmmod  */
+
+/* Function to insert the kernel module into the system*/
+static int insmod(const char *filename, const char *args)
+{
+       void *module;
+       unsigned int size;
+       int ret = -1;
+
+       UIM_START_FUNC();
+
+       module = (void *)load_file(filename, &size);
+       if (!module)
+               return ret;
+
+       ret = init_module(module, size, args);
+       free(module);
+
+       return ret;
+}
+
+/* Function to remove the kernel module from the system*/
+static int rmmod(const char *modname)
+{
+       int ret = -1;
+       int maxtry = MAX_TRY;
+
+       UIM_START_FUNC();
+
+       /* Retry MAX_TRY number of times in case of
+        * failure
+        */
+       while (maxtry-- > 0) {
+               ret = delete_module(modname, O_NONBLOCK | O_EXCL);
+               if (ret < 0 && errno == EAGAIN)
+                       sleep(1);
+               else
+                       break;
+       }
+
+       /* Failed to remove the module
+       */
+       if (ret != 0)
+               UIM_ERR("Unable to unload driver module \"%s\": %s",
+                               modname, strerror(errno));
+       return ret;
+}
+#endif /* ANDROID */
 
 /*****************************************************************************/
 /* Function to read the HCI event from the given file descriptor
@@ -85,7 +144,7 @@ int read_hci_event(int fd, unsigned char *buf, int size)
        int count = 0;
        int reading = 1;
        int rd_retry_count = 0;
-       struct timespec tm = { 0, 50 * 1000 * 1000 };
+       struct timespec tm = {0, 50*1000*1000};
 
        UIM_START_FUNC();
 
@@ -147,7 +206,7 @@ static int read_command_complete(int fd, unsigned short opcode)
        UIM_START_FUNC();
 
        UIM_VER(" Command complete started");
-       if (read_hci_event(fd, (unsigned char *) &resp, sizeof(resp)) < 0) {
+       if (read_hci_event(fd, (unsigned char *)&resp, sizeof(resp)) < 0) {
                UIM_ERR(" Invalid response");
                return -1;
        }
@@ -156,7 +215,7 @@ static int read_command_complete(int fd, unsigned short opcode)
        if (resp.uart_prefix != HCI_EVENT_PKT) {
                UIM_ERR
                        (" Error in response: not an event packet, but 0x%02x!",
-                               resp.uart_prefix);
+                        resp.uart_prefix);
                return -1;
        }
 
@@ -165,7 +224,7 @@ static int read_command_complete(int fd, unsigned short opcode)
                /* event must be event-complete */
                UIM_ERR
                        (" Error in response: not a cmd-complete event,but 0x%02x!",
-                               resp.hci_hdr.evt);
+                        resp.hci_hdr.evt);
                return -1;
        }
 
@@ -176,7 +235,7 @@ static int read_command_complete(int fd, unsigned short opcode)
                return -1;
        }
 
-       if (resp.cmd_complete.opcode != (unsigned short) opcode) {
+       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;
@@ -192,11 +251,12 @@ static int read_command_complete(int fd, unsigned short opcode)
  * 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)
+static int set_baud_rate()
 {
-       UIM_START_FUNC();
        struct termios ti;
 
+       UIM_START_FUNC();
+
        tcflush(dev_fd, TCIOFLUSH);
 
        /* Get the attributes of UART */
@@ -233,22 +293,26 @@ static int set_baud_rate(int dev_fd)
  * 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)
+static int set_custom_baud_rate(int cust_baud_rate, unsigned char flow_ctrl)
 {
        UIM_START_FUNC();
 
        struct termios ti;
        struct termios2 ti2;
 
-       /* Flush non-transmitted output data,
-        * non-read input data or both*/
-       tcflush(dev_fd, TCIOFLUSH);
        /* Get the attributes of UART */
        if (tcgetattr(dev_fd, &ti) < 0) {
                UIM_ERR(" Can't get port settings");
                return -1;
        }
 
+       UIM_VER(" Changing baud rate to %u, flow control to %u",
+                       cust_baud_rate, flow_ctrl);
+
+       /* Flush non-transmitted output data,
+        * non-read input data or both*/
+       tcflush(dev_fd, TCIOFLUSH);
+
        /*Set the UART flow control */
        if (flow_ctrl)
                ti.c_cflag |= CRTSCTS;
@@ -270,101 +334,103 @@ static int set_custom_baud_rate(int dev_fd, int baud_rate, int flow_ctrl)
        ioctl(dev_fd, TCGETS2, &ti2);
        ti2.c_cflag &= ~CBAUD;
        ti2.c_cflag |= BOTHER;
-       ti2.c_ospeed = baud_rate;
+       ti2.c_ospeed = cust_baud_rate;
        ioctl(dev_fd, TCSETS2, &ti2);
 
        UIM_DBG(" set_custom_baud_rate() done");
        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
+/*
+ * Handling the Signals sent from the Kernel Init Manager.
+ * After receiving the indication from rfkill subsystem, configure the
+ * baud rate, flow control and Install the N_TI_WL line discipline
  */
 int st_uart_config(unsigned char install)
 {
-       int ldisc, len, fd, flow_ctrl;
-       unsigned char buf[UART_DEV_NAME_LEN+1];
-       uim_speed_change_cmd cmd;
-       char uart_dev_name[UART_DEV_NAME_LEN+1];
-       long cust_baud_rate;
+       int ldisc, len, fd;
+       unsigned char uart_dev_name[32];
+       unsigned char buf[32];
+       unsigned long cust_baud_rate;
+       unsigned int flow_ctrl;
 
+       uim_speed_change_cmd cmd;
        uim_bdaddr_change_cmd addr_cmd;
 
        UIM_START_FUNC();
 
        if (install == '1') {
-               memset(buf, 0, UART_DEV_NAME_LEN+1);
+               UIM_DBG("install set to 1");
+               memset(buf, 0, 32);
                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);
+               len = read(fd, buf, 32);
                if (len < 0) {
                        UIM_ERR("read err (%s)", strerror(errno));
                        close(fd);
                        return len;
                }
-               sscanf((const char *) buf, "%s", uart_dev_name);
+               sscanf((const char*)buf, "%s", uart_dev_name);
                close(fd);
 
-               memset(buf, 0, UART_DEV_NAME_LEN+1);
+               memset(buf, 0, 32);
                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);
+               len = read(fd, buf, 32);
                if (len < 0) {
                        UIM_ERR("read err (%s)", strerror(errno));
                        close(fd);
                        return len;
                }
                close(fd);
-               sscanf((const char *) buf, "%ld", &cust_baud_rate);
+               sscanf((const char*)buf, "%ld", &cust_baud_rate);
 
-               memset(buf, 0, UART_DEV_NAME_LEN+1);
+               memset(buf, 0, 32);
                fd = open(FLOW_CTRL_SYSFS, O_RDONLY);
                if (fd < 0) {
                        UIM_ERR("Can't open %s", FLOW_CTRL_SYSFS);
-                       /* As fd was not opened, it's not necessary to close it */
+                       close(fd);
                        return -1;
                }
-               len = read(fd, buf, UART_DEV_NAME_LEN);
+               len = read(fd, buf, 32);
                if (len < 0) {
                        UIM_ERR("read err (%s)", strerror(errno));
                        close(fd);
                        return len;
                }
                close(fd);
-               sscanf((const char *) buf, "%d", &flow_ctrl);
+               sscanf((const char*)buf, "%u", &flow_ctrl);
 
-               UIM_VER(" signal received, opening %s", uart_dev_name);
+               if (dev_fd != -1) {
+                       UIM_ERR("opening %s, while already open", uart_dev_name);
+                       cleanup(-1);
+               }
 
-               dev_fd = open(uart_dev_name, O_RDWR);
+               dev_fd = open((const char*) uart_dev_name, O_RDWR);
                if (dev_fd < 0) {
-                       UIM_ERR("Can't open %s", uart_dev_name);
+                       UIM_ERR(" Can't open %s", uart_dev_name);
                        return -1;
                }
-
-               UIM_VER(" Setting default baudrate");
-
                /*
                 * 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);
+               if (set_baud_rate() < 0) {
+                       UIM_ERR(" set_baudrate() failed");
+                       cleanup(-1);
                        return -1;
                }
 
-               fcntl(dev_fd, F_SETFL, fcntl(dev_fd, F_GETFL) | O_NONBLOCK);
-               /* Set only the custom baud rate */
+               fcntl(dev_fd, F_SETFL,fcntl(dev_fd, F_GETFL) | O_NONBLOCK);
+               /* Set only thecustom baud rate */
                if (cust_baud_rate != 115200) {
 
-                       UIM_VER("Setting speed to %ld", cust_baud_rate);
                        /* Forming the packet for Change speed command */
                        cmd.uart_prefix = HCI_COMMAND_PKT;
                        cmd.hci_hdr.opcode = HCI_HDR_OPCODE;
@@ -375,32 +441,31 @@ int st_uart_config(unsigned char install)
                         * This will change the UART speed at the controller
                         * side
                         */
-                       UIM_VER(" Setting speed to %d", cust_baud_rate);
+                       UIM_VER(" Setting speed to %ld", cust_baud_rate);
                        len = write(dev_fd, &cmd, sizeof(cmd));
                        if (len < 0) {
-                               UIM_ERR("Failed to write speed-set command");
-                               close(dev_fd);
+                               UIM_ERR(" Failed to write speed-set command");
+                               cleanup(-1);
                                return -1;
                        }
 
                        /* Read the response for the Change speed command */
                        if (read_command_complete(dev_fd, HCI_HDR_OPCODE) < 0) {
-                               close(dev_fd);
+                               cleanup(-1);
                                return -1;
                        }
 
-                       UIM_VER(" Speed changed to %d", cust_baud_rate);
+                       UIM_VER(" Speed changed to %ld", cust_baud_rate);
 
                        /* 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);
-
+                       if (set_custom_baud_rate(cust_baud_rate, flow_ctrl) < 0) {
+                               UIM_ERR(" set_custom_baud_rate() failed");
+                               cleanup(-1);
                                return -1;
                        }
 
                        /* Set the uim BD address */
-                       if (bd_addr) {
+                       if (uim_bd_address[0] != 0) {
 
                                memset(&addr_cmd, 0, sizeof(addr_cmd));
                                /* Forming the packet for change BD address command*/
@@ -415,161 +480,317 @@ int st_uart_config(unsigned char install)
                                 */
                                len = write(dev_fd, &addr_cmd, sizeof(addr_cmd));
                                if (len < 0) {
-                                       UIM_ERR("Failed to write BD address command");
-                                       close(dev_fd);
+                                       UIM_ERR(" Failed to write BD address command");
+                                       cleanup(-1);
                                        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);
+                                       cleanup(-1);
                                        return -1;
                                }
-                               UIM_VER("BD address changed to "
-                                               "%02X:%02X:%02X:%02X:%02X:%02X", bd_addr->b[0],
-                                               bd_addr->b[1], bd_addr->b[2], bd_addr->b[3],
-                                               bd_addr->b[4], bd_addr->b[5]);
+
+                               UIM_VER(" BD address changed to %s", uim_bd_address);
                        }
 #ifdef UIM_DEBUG
-                       read_firmware_version(dev_fd);
+                       read_firmware_version();
 #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;
+               ldisc = 22;
                if (ioctl(dev_fd, TIOCSETD, &ldisc) < 0) {
                        UIM_ERR(" Can't set line discipline");
-                       close(dev_fd);
+                       cleanup(-1);
                        return -1;
                }
-               UIM_DBG("Installed N_TI_WL Line displine");
-       } else {
-               UIM_DBG("Un-Installed N_TI_WL Line displine");
+               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);
+               cleanup(0);
+               dev_fd = -1;
        }
        return 0;
 }
 
-/* Function to convert the BD address from ascii to hex value */
-bdaddr_t *strtoba(const char *str)
+int remove_modules()
 {
-       uint8_t *ba = malloc(sizeof(bdaddr_t));
-       unsigned int tmp_bd[BD_ADDR_BIN_LEN];
-       int i;
+       int err = 0;
+
+#ifdef ANDROID
+       UIM_VER(" Removing gps_drv ");
+       if (rmmod("gps_drv") != 0) {
+               UIM_ERR(" Error removing gps_drv module");
+               err = -1;
+       } else {
+               UIM_DBG(" Removed gps_drv module");
+       }
+
+       UIM_VER(" Removing fm_drv ");
+       if (rmmod("fm_drv") != 0) {
+               UIM_ERR(" Error removing fm_drv module");
+               err = -1;
+       } else {
+               UIM_DBG(" Removed fm_drv module");
+       }
+       UIM_DBG(" Removed fm_drv module");
+
+       UIM_VER(" Removing btwilink ");
+       if (rmmod("btwilink") != 0) {
+               UIM_ERR(" Error removing btwilink module");
+               err = -1;
+       } else {
+               UIM_DBG(" Removed btwilink module");
+       }
+       UIM_DBG(" Removed btwilink module");
+
+       /*Remove the Shared Transport */
+       UIM_VER(" Removing st_drv ");
+       if (rmmod("st_drv") != 0) {
+               UIM_ERR(" Error removing st_drv module");
+               err = -1;
+       } else {
+               UIM_DBG(" Removed st_drv module ");
+       }
+       UIM_DBG(" Removed st_drv module ");
+#else
+       UIM_VER(" Removing btwilink ");
+       if (system("rmmod btwilink") != 0) {
+               UIM_ERR(" Error removing btwilink module");
+               err = -1;
+       } else {
+               UIM_DBG(" Removed btwilink module");
+       }
+
+       UIM_VER(" Removing fm_drv ");
+       if (system("rmmod fm_drv") != 0) {
+               UIM_ERR(" Error removing fm_drv module");
+               err = -1;
+       } else {
+               UIM_DBG(" Removed fm_drv module ");
+       }
 
+       /*Remove the Shared Transport */
+       UIM_VER(" Removing st_drv ");
+       if (system("rmmod st_drv") != 0) {
+               UIM_ERR(" Error removing st_drv module");
+               err = -1;
+       } else {
+               UIM_DBG(" Removed st_drv module ");
+       }
+#endif
+       return err;
+}
 
-       if (ba) {
-               memset(tmp_bd, 0, BD_ADDR_BIN_LEN);
-               if (sscanf(str, "%02X:%02X:%02X:%02X:%02X:%02X",
-                               &tmp_bd[0], &tmp_bd[1], &tmp_bd[2],
-                               &tmp_bd[3], &tmp_bd[4], &tmp_bd[5]) != sizeof(bdaddr_t)) {
-                       free (ba);
-                       ba = NULL;
-                       goto exit;
+int change_rfkill_perms(void)
+{
+       int fd, id, sz;
+       char path[64];
+       char buf[16];
+       for (id = 0; id < 50; id++) {
+               snprintf(path, sizeof(path), "/sys/class/rfkill/rfkill%d/type", id);
+               fd = open(path, O_RDONLY);
+               if (fd < 0) {
+                       UIM_DBG("open(%s) failed: %s (%d)\n", path, strerror(errno), errno);
+                       continue;
                }
-               for (i=0;i<BD_ADDR_BIN_LEN;i++){
-                       if(tmp_bd[i] > 255){
-                               free (ba);
-                               ba = NULL;
-                               goto exit;
-                       }
-                       ba[i] = (uint8_t) tmp_bd[i];
+               sz = read(fd, &buf, sizeof(buf));
+               close(fd);
+               if (sz >= 9 && memcmp(buf, "bluetooth", 9) == 0) {
+                       UIM_DBG("found bluetooth rfkill entry @ %d\n", id);
+                       break;
                }
+       }
+       if (id == 50) {
+               return -1;
+       }
+#ifdef ANDROID
+       sprintf(path, "/sys/class/rfkill/rfkill%d/state", id);
+       sz = chown(path, AID_BLUETOOTH, AID_BLUETOOTH);
+       if (sz < 0) {
+               UIM_ERR("change mode failed for %s (%d)\n", path, errno);
+               return -1;
+       }
+#endif
+       /*
+        * bluetooth group's user system needs write permission
+        */
+       sz = chmod(path, 0660);
+       if (sz < 0) {
+               UIM_ERR("change mode failed for %s (%d)\n", path, errno);
+               return -1;
+       }
+       UIM_DBG("changed permissions for %s(%d) \n", path, sz);
+       /* end of change_perms */
+
+       return 0;
+}
 
+void *bt_malloc(size_t size)
+{
+       return malloc(size);
+}
+
+/* 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 = bt_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++;
        }
-exit:
+
        return (bdaddr_t *) ba;
 }
 
 /*****************************************************************************/
 int main(int argc, char *argv[])
 {
-       int st_fd, err;
-       unsigned char install, previous;
-       struct pollfd p;
-       unsigned int i;
-       /* List of invalid BD addresses */
-       const bdaddr_t bd_address_ignored[] = {
-                       { { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 } },
-                       { { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } } };
+       int st_fd,err;
+       struct stat file_stat;
+#ifndef ANDROID        /* used on ubuntu */
+       char *tist_ko_path;
+       struct utsname name;
+#endif
+       struct pollfd   p;
+       unsigned char install;
 
        UIM_START_FUNC();
-       bd_addr = NULL;
        err = 0;
 
        /* Parse the user input */
-       if ((argc > 2)) {
-               UIM_ERR("Invalid arguments");
-               UIM_ERR("Usage: uim <bd address>");
-               return -1;
-       }
        if (argc == 2) {
-               if (strlen(argv[1]) != BD_ADDR_LEN) {
-                       UIM_ERR("Usage: uim XX:XX:XX:XX:XX:XX");
-                       return -1;
-               }
+               memset(&uim_bd_address, 0, sizeof(uim_bd_address));
                /* BD address passed as string in xx:xx:xx:xx:xx:xx format */
-               strncpy(uim_bd_address, argv[1], BD_ADDR_LEN+1);
+               strcpy(uim_bd_address, argv[1]);
                bd_addr = strtoba(uim_bd_address);
+       } else if (argc != 1) {
+               UIM_ERR(" Invalid arguements");
+               UIM_ERR(" Usage: uim <bd address>");
+               return -1;
        }
 
-       if (bd_addr) {
-               /* Check if read value has to be ignored */
-               for (i = 0; i < (sizeof(bd_address_ignored) / sizeof(bdaddr_t)); i++) {
+#ifndef ANDROID
+       if (uname (&name) == -1) {
+               UIM_ERR("cannot get kernel release name");
+               return -1;
+       }
+#else  /* if ANDROID */
 
-                       if (memcmp(&bd_address_ignored[i], bd_addr, sizeof(bdaddr_t)) == 0) {
+       if (0 == lstat("/st_drv.ko", &file_stat)) {
+               if (insmod("/st_drv.ko", "") < 0) {
+                       UIM_ERR(" Error inserting st_drv module");
+                       return -1;
+               } else {
+                       UIM_DBG(" Inserted st_drv module");
+               }
+       } else {
+               if (0 == lstat(INSTALL_SYSFS_ENTRY, &file_stat)) {
+                       UIM_DBG("ST built into the kernel ?");
+               } else {
+                       UIM_ERR("BT/FM/GPS would be unavailable on system");
+                       return -1;
+               }
+       }
 
-                               UIM_DBG("Stored value "
-                                               "%02X:%02X:%02X:%02X:%02X:%02X was ignored",
-                                               bd_addr->b[0], bd_addr->b[1], bd_addr->b[2],
-                                               bd_addr->b[3], bd_addr->b[4], bd_addr->b[5]);
-                               UIM_DBG("Using default chip bd address");
+       if (0 == lstat("/btwilink.ko", &file_stat)) {
+               if (insmod("/btwilink.ko", "") < 0) {
+                       UIM_ERR(" Error inserting btwilink module, NO BT? ");
+               } else {
+                       UIM_DBG(" Inserted btwilink module");
+               }
+       } else {
+               UIM_DBG("BT driver module un-available... ");
+               UIM_DBG("BT driver built into the kernel ?");
+       }
 
-                               free(bd_addr);
-                               bd_addr = NULL;
+       if (0 == lstat("/fm_drv.ko", &file_stat)) {
+               if (insmod("/fm_drv.ko", "") < 0) {
+                       UIM_ERR(" Error inserting fm_drv module, NO FM? ");
+               } else {
+                       UIM_DBG(" Inserted fm_drv module");
+               }
+       } else {
+               UIM_DBG("FM driver module un-available... ");
+               UIM_DBG("FM driver built into the kernel ?");
+       }
 
-                               break;
-                       }
+       if (0 == lstat("/gps_drv.ko", &file_stat)) {
+               if (insmod("/gps_drv.ko", "") < 0) {
+                       UIM_ERR(" Error inserting gps_drv module, NO GPS? ");
+               } else {
+                       UIM_DBG(" Inserted gps_drv module");
                }
-               if (bd_addr)
-                       UIM_DBG("Using %s bd address", uim_bd_address);
-       } else
-               UIM_DBG("Using default chip bd address");
+       } else {
+               UIM_DBG("GPS driver module un-available... ");
+               UIM_DBG("GPS driver built into the kernel ?");
+       }
 
-       line_discipline = N_TI_WL;
+       if (0 == lstat("/fm_v4l2_drv.ko", &file_stat)) {
+               if (insmod("/fm_v4l2_drv.ko", "") < 0) {
+                       UIM_ERR(" Error inserting fm_v4l2_drv module, NO FM? ");
+               } else {
+                       UIM_DBG(" Inserted fm_v4l2_drv module");
+               }
+       } else {
+               UIM_DBG("FM V4L2 driver module un-available... ");
+               UIM_DBG("FM V4L2 driver built into the kernel ?");
+       }
+       /* Change the permissions for v4l2 Fm device node */
+       if ((0 == lstat("/dev/radio0", &file_stat)) && chmod("/dev/radio0", 0666) < 0) {
+               UIM_ERR("unable to chmod /dev/radio0, might not exist");
+       }
+       if ((0 == lstat("/dev/tifm", &file_stat)) && chmod("/dev/tifm", 0666) < 0) {
+               UIM_ERR("unable to chmod /dev/tifm, might not exist");
+       }
+       /* change rfkill perms after insertion of BT driver which asks
+        * the Bluetooth sub-system to create the rfkill device of type
+        * "bluetooth"
+        */
+       if (change_rfkill_perms() < 0) {
+               /* possible error condition */
+               UIM_ERR("rfkill not enabled in st_drv - BT on from UI might fail\n");
+       }
 
+#endif /* ANDROID */
+       /* rfkill device's open/poll/read */
        st_fd = open(INSTALL_SYSFS_ENTRY, O_RDONLY);
        if (st_fd < 0) {
-               UIM_DBG("unable to open %s(%s)", INSTALL_SYSFS_ENTRY, strerror(errno));
+               UIM_DBG("unable to open %s (%s)", INSTALL_SYSFS_ENTRY,
+                               strerror(errno));
+               remove_modules();
                return -1;
        }
 
-       /* read to start proper poll */
+RE_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...");
+       if ((err > 0) && (install == '1')) {
+               UIM_DBG("install already set");
                st_uart_config(install);
        }
 
-RE_POLL:
-
-       UIM_DBG("begin polling...");
-
        memset(&p, 0, sizeof(p));
        p.fd = st_fd;
-       p.events = POLLERR | POLLPRI;
+       /* sysfs entries can only break poll for following events */
+       p.events = POLLERR | POLLHUP;
 
        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)
@@ -579,26 +800,29 @@ RE_POLL:
        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));
+               UIM_ERR("re-opening %s failed: %s", INSTALL_SYSFS_ENTRY,
+               strerror(errno));
                return -1;
        }
 
-       if (!exiting) {
-               previous = install;
+       if (!exiting)
+       {
                err = read(st_fd, &install, 1);
-               UIM_DBG("read %c from install (previously was %c)\n", install, previous);
-               if (err > 0)
-                       if (previous != install)
-                               st_uart_config(install);
-                       else
-                               UIM_DBG("lost install event, retry later");
-
+               if (err <= 0) {
+                       UIM_ERR("reading %s failed: %s", INSTALL_SYSFS_ENTRY,
+                                       strerror(errno));
+                       goto RE_POLL;
+               }
+               st_uart_config(install);
                goto RE_POLL;
        }
 
+       if(remove_modules() < 0) {
+               UIM_ERR(" Error removing modules ");
+               close(st_fd);
+               return -1;
+       }
+
        close(st_fd);
-       /* Free resources */
-       if (bd_addr)
-               free(bd_addr);
        return 0;
 }
diff --git a/uim.h b/uim.h
index 9a544fe..dbb318d 100644 (file)
--- a/uim.h
+++ b/uim.h
 #ifndef UIM_H
 #define UIM_H
 
-#ifdef ANDROID
-#include <cutils/log.h>
-#include <cutils/properties.h>
-#define LOG_TAG "UIM"
-#endif
-
-/* 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
+#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
+#define TCGETS2      _IOR('T',0x2A, struct termios2)
+#define TCSETS2      _IOW('T',0x2B, struct termios2)
 
 /*HCI Command and Event information*/
-#define HCI_HDR_OPCODE         0xff36
-#define WRITE_BD_ADDR_OPCODE    0xFC06
-#define RESP_PREFIX            0x04
-#define MAX_TRY                        10
+#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
+#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_EVENT_HDR_SIZE              2
+#define HCI_COMMAND_HDR_SIZE            3
+#define UIM_WRITE_BD_ADDR_CP_SIZE       6
+
 
 /* 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
-/* BD address length in binary */
-#define BD_ADDR_BIN_LEN 6
-/* Path to bd address provisioning file */
-#define BD_PATH "/system/etc/bluetooth"
-
-
-/* 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 EVT_CMD_COMPLETE_SIZE   3
+#define EVT_CMD_STATUS_SIZE     4
+#define EVT_CMD_COMPLETE        0x0E
+#define EVT_CMD_STATUS          0x0F
+
 
-#ifdef ANDROID
 #define VERBOSE
-/*Debug logs*/
-#define UIM_ERR(fmt, arg...)  LOGE(fmt"\n" , ##arg)
-#if defined(UIM_DEBUG)         /* limited debug messages */
-#define UIM_START_FUNC()      LOGV("Inside %s", __FUNCTION__)
-#define UIM_DBG(fmt, arg...)  LOGD(fmt"\n" , ## arg)
+#ifdef ANDROID
+#define LOG_TAG "uim-sysfs"
+#define UIM_ERR(fmt, arg...)  ALOGE("uim:"fmt"\n" , ##arg)
+#if defined(UIM_DEBUG)          /* limited debug messages */
+#define UIM_START_FUNC()      ALOGE("uim: Inside %s", __FUNCTION__)
+#define UIM_DBG(fmt, arg...)  ALOGE("uim:"fmt"\n" , ## arg)
 #define UIM_VER(fmt, arg...)
-#elif defined(VERBOSE)         /* very verbose */
-#define UIM_START_FUNC()      LOGV("@ %s\n", __FUNCTION__)
-#define UIM_DBG(fmt, arg...)  LOGD(fmt"\n" , ## arg)
-#define UIM_VER(fmt, arg...)  LOGV(fmt"\n" , ## arg)
+#elif defined(VERBOSE)          /* very verbose */
+#define UIM_START_FUNC()      ALOGE("uim: Inside %s", __FUNCTION__)
+#define UIM_DBG(fmt, arg...)  ALOGE("uim:"fmt"\n" , ## arg)
+#define UIM_VER(fmt, arg...)  ALOGE("uim:"fmt"\n" , ## arg)
 #else /* error msgs only */
 #define UIM_START_FUNC()
 #define UIM_DBG(fmt, arg...)
 #define UIM_VER(fmt, arg...)
 #endif
 #else
-#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
-#endif
+#define UIM_ERR(fmt, arg...)
+#endif  /* ANDROID */
 
 /*Termios2 structure for setting the Custom baud rate*/
 struct termios2 {
-        tcflag_t c_iflag;       /* input mode flags */
-        tcflag_t c_oflag;       /* output mode flags */
-        tcflag_t c_cflag;       /* control mode flags */
-        tcflag_t c_lflag;       /* local mode flags */
-        cc_t c_line;            /* line discipline */
-        cc_t c_cc[ARM_NCCS];    /* control characters */
-        speed_t c_ispeed;       /* input speed */
-        speed_t c_ospeed;       /* output speed */
+       tcflag_t c_iflag;       /* input mode flags */
+       tcflag_t c_oflag;       /* output mode flags */
+       tcflag_t c_cflag;       /* control mode flags */
+       tcflag_t c_lflag;       /* local mode flags */
+       cc_t c_line;            /* line discipline */
+       cc_t c_cc[ARM_NCCS];    /* control characters */
+       speed_t c_ispeed;       /* input speed */
+       speed_t c_ospeed;       /* output speed */
 };
 
 /* HCI command header*/
@@ -180,6 +137,16 @@ typedef struct {
        uint8_t uart_prefix;
        hci_command_hdr hci_hdr;
        bdaddr_t addr;
-} __attribute__ ((packed)) uim_bdaddr_change_cmd;\
+} __attribute__ ((packed)) uim_bdaddr_change_cmd;
+
+#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"
+
+/* Functions to insert and remove the kernel modules from the system*/
+extern int init_module(void *, unsigned int, const char *);
+extern int delete_module(const char *, unsigned int);
+extern int load_file(const char *, unsigned int *);
 
 #endif /* UIM_H */