From fd9e188fdf2f6c58a6e7f126c65e04836f596c02 Mon Sep 17 00:00:00 2001 From: Sehong Na Date: Sat, 31 May 2014 13:02:17 +0900 Subject: [PATCH] Initialize Tizen 2.3 --- LICENSE | 339 +++++++++++++++++++++ packaging/uim.changes | 14 + packaging/uim.spec | 27 ++ uim.c | 828 ++++++++++++++++++++++++++++++++++++++++++++++++++ uim.h | 152 +++++++++ 5 files changed, 1360 insertions(+) create mode 100644 LICENSE create mode 100644 packaging/uim.changes create mode 100644 packaging/uim.spec create mode 100644 uim.c create mode 100644 uim.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..68bab0e --- /dev/null +++ b/LICENSE @@ -0,0 +1,339 @@ +GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Lesser General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + Tiny instant messenger + Copyright (C) 2013 ushmal + + 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., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) year name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + {signature of Ty Coon}, 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. diff --git a/packaging/uim.changes b/packaging/uim.changes new file mode 100644 index 0000000..a6c2d10 --- /dev/null +++ b/packaging/uim.changes @@ -0,0 +1,14 @@ +* Wed Sept 19 2012 Zheng, wu +- fix license in spec file + +* Tue Sept 11 2012 Zheng, wu +- Fix the bluetooth can't pair with the other device issue. + +* Fri Aug 31 2012 Patrick McCarty dbd7662 +- Repackage the sysvinit script and symlink + +* Tue Aug 07 2012 Patrick McCarty 3dab3d8 +- systemd: move the module loading to a new service file + +* Mon Aug 06 2012 Patrick McCarty 925f272 +- Add/install a systemd service file diff --git a/packaging/uim.spec b/packaging/uim.spec new file mode 100644 index 0000000..2c3549a --- /dev/null +++ b/packaging/uim.spec @@ -0,0 +1,27 @@ +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/ +mkdir -p %{buildroot}/usr/share/license +cp LICENSE %{buildroot}/usr/share/license/%{name} + +%files +%attr(0755,-,-) /bin/uim +/usr/share/license/%{name} diff --git a/uim.c b/uim.c new file mode 100644 index 0000000..60e3400 --- /dev/null +++ b/uim.c @@ -0,0 +1,828 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "uim.h" +#ifdef ANDROID +#include +#include +#endif + + +/* Maintains the exit state of UIM*/ +static int exiting; + +/* BD address as string and a pointer to array of hex bytes */ +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 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 /* 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 + * + * 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, but 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,but 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() +{ + struct termios ti; + + UIM_START_FUNC(); + + 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 cust_baud_rate, unsigned char flow_ctrl) +{ + UIM_START_FUNC(); + + struct termios ti; + struct termios2 ti2; + + /* 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; + 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 = cust_baud_rate; + ioctl(dev_fd, TCSETS2, &ti2); + + UIM_DBG(" set_custom_baud_rate() done"); + return 0; +} + +/* + * 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; + 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') { + 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, 32); + 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, 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, 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); + + memset(buf, 0, 32); + 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, 32); + if (len < 0) { + UIM_ERR("read err (%s)", strerror(errno)); + close(fd); + return len; + } + close(fd); + sscanf((const char*)buf, "%u", &flow_ctrl); + + if (dev_fd != -1) { + UIM_ERR("opening %s, while already open", uart_dev_name); + cleanup(-1); + } + + dev_fd = open((const char*) 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() < 0) { + UIM_ERR(" set_baudrate() failed"); + cleanup(-1); + return -1; + } + + fcntl(dev_fd, F_SETFL,fcntl(dev_fd, F_GETFL) | O_NONBLOCK); + /* Set only thecustom baud rate */ + if (cust_baud_rate != 115200) { + + /* 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 long); + cmd.speed = cust_baud_rate; + + /* Writing the change speed command to the UART + * This will change the UART speed at the controller + * side + */ + 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"); + cleanup(-1); + return -1; + } + + /* Read the response for the Change speed command */ + if (read_command_complete(dev_fd, HCI_HDR_OPCODE) < 0) { + cleanup(-1); + return -1; + } + + UIM_VER(" Speed changed to %ld", cust_baud_rate); + + /* Set the actual custom baud rate at the host side */ + 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 (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"); + cleanup(-1); + return -1; + } + + /* Read the response for the change BD address command */ + if (read_command_complete(dev_fd, WRITE_BD_ADDR_OPCODE) < 0) { + cleanup(-1); + return -1; + } + + UIM_VER(" BD address changed to %s", uim_bd_address); + } +#ifdef UIM_DEBUG + 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 = 22; + if (ioctl(dev_fd, TIOCSETD, &ldisc) < 0) { + UIM_ERR(" Can't set line discipline"); + cleanup(-1); + 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 */ + cleanup(0); + dev_fd = -1; + } + return 0; +} + +int remove_modules() +{ + 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; +} + +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; + } + 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++; + } + + return (bdaddr_t *) ba; +} + +/*****************************************************************************/ +int main(int argc, char *argv[]) +{ + 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(); + err = 0; + + /* Parse the user input */ + if (argc == 2) { + memset(&uim_bd_address, 0, sizeof(uim_bd_address)); + /* BD address passed as string in xx:xx:xx:xx:xx:xx format */ + strcpy(uim_bd_address, argv[1]); + bd_addr = strtoba(uim_bd_address); + } else if (argc != 1) { + UIM_ERR(" Invalid arguements"); + UIM_ERR(" Usage: uim "); + return -1; + } + +#ifndef ANDROID + if (uname (&name) == -1) { + UIM_ERR("cannot get kernel release name"); + return -1; + } +#else /* if ANDROID */ + + 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; + } + } + + 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 ?"); + } + + 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 ?"); + } + + 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"); + } + } else { + UIM_DBG("GPS driver module un-available... "); + UIM_DBG("GPS driver built into the kernel ?"); + } + + 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)); + remove_modules(); + return -1; + } + +RE_POLL: + err = read(st_fd, &install, 1); + if ((err > 0) && (install == '1')) { + UIM_DBG("install already set"); + st_uart_config(install); + } + + memset(&p, 0, sizeof(p)); + p.fd = st_fd; + /* sysfs entries can only break poll for following events */ + p.events = POLLERR | POLLHUP; + + while (!exiting) { + p.revents = 0; + err = poll(&p, 1, -1); + 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_ERR("re-opening %s failed: %s", INSTALL_SYSFS_ENTRY, + strerror(errno)); + return -1; + } + + if (!exiting) + { + err = read(st_fd, &install, 1); + 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); + return 0; +} diff --git a/uim.h b/uim.h new file mode 100644 index 0000000..dbb318d --- /dev/null +++ b/uim.h @@ -0,0 +1,152 @@ +/* + * 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 + +/* Paramaters to set the baud rate*/ +#define FLOW_CTL 0x0001 +#define BOTHER 0x00001000 +#define ARM_NCCS 19 + +#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 + +/* 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 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 + + +#define VERBOSE +#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() 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 UIM_START_FUNC() +#define UIM_DBG(fmt, arg...) +#define UIM_VER(fmt, arg...) +#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 */ +}; + +/* 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; + +#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 */ -- 2.7.4