diff options
author | Jonathan DE CESCO <jonathanc@ti.com> | 2010-10-26 11:28:10 +0200 |
---|---|---|
committer | Jonathan DE CESCO <jonathanc@ti.com> | 2010-10-26 11:28:10 +0200 |
commit | c334987f4ebc96ae8c6a0aa95378e1bb223b0a00 (patch) | |
tree | 3cc2ff93860363c0600fa051aaa950676efdf898 | |
parent | 683cc95127f2c78a8b32e653b9cb7d79958476a3 (diff) | |
download | uim-c334987f4ebc96ae8c6a0aa95378e1bb223b0a00.tar.gz |
First UIM source commit
-rw-r--r-- | uim.c | 568 | ||||
-rw-r--r-- | uim.h | 140 | ||||
-rw-r--r-- | uim.spec | 58 | ||||
-rw-r--r-- | uim.tgz | bin | 0 -> 28836 bytes | |||
-rw-r--r-- | uim.yaml | 18 |
5 files changed, 784 insertions, 0 deletions
@@ -0,0 +1,568 @@ +/* + * 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 "uim.h" + +/* Maintains the exit state of UIM*/ +static int exiting; + +/* UART configuration parameters*/ +int uart_flow_control; +int cust_baud_rate; +char uart_dev_name[15]; +unsigned int uart_baud_rate; +struct termios ti; +int line_discipline; + +/* File descriptor for the UART device*/ +int dev_fd; + +/* Maintains the state of N_TI_WL line discipline installation*/ +unsigned char st_state = INSTALL_N_TI_WL; +unsigned char prev_st_state = UNINSTALL_N_TI_WL; + +struct rfkill_event { +uint32_t idx; +uint8_t type; +uint8_t op; +uint8_t soft, hard; +} __packed; +struct rfkill_event rf_event; +int rfkill_idx; + +/*****************************************************************************/ +#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 + +/*****************************************************************************/ +/* 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) +{ + struct 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 + (" Err 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 + (" Err in response: not a cmdcomplete evt,but 0x%02x!", + resp.hci_hdr.evt); + return -1; + } + + if (resp.hci_hdr.plen < 4) { + /* plen >= 4 for EVT_CMD_COMPLETE */ + UIM_ERR(" Err 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(" Err 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() +{ + 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() +{ + UIM_START_FUNC(); + + struct termios2 ti2; + + UIM_VER(" Changing baud rate to %u, flow control to %u", + cust_baud_rate, uart_flow_control); + + /* Flush non-transmitted output data, + * non-read input data or both*/ + tcflush(dev_fd, TCIOFLUSH); + + /*Set the UART flow control */ + if (uart_flow_control) + 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 signals, configure the baud rate, flow + * control and Install the N_TI_WL line discipline + */ +int st_sig_handler(int signo) +{ + int ldisc, len; + struct uim_speed_change_cmd cmd; + + UIM_START_FUNC(); + + /* Raise a signal after when UIM is killed. + * This will exit UIM, and remove the inserted kernel + * modules + */ + if (signo == SIGINT) { + UIM_DBG(" Exiting. . ."); + exiting = 1; + return -1; + } + + /* Install the line discipline when the signal is received by UIM. + * Whenever the first protocol tries to register with the ST core, the + * ST KIM will send a signal SIGUSR2 to the UIM to install the N_TI_WL + * line discipline and do the host side UART configurations. + * + * On failure, ST KIM's line discipline installation times out, and the + * relevant protocol register fails + */ + if (st_state == INSTALL_N_TI_WL) { + 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() < 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 thecustom baud rate */ + if (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 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 %d", cust_baud_rate); + 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 changed to %d", cust_baud_rate); + + /* Set the actual custom baud rate at the host side */ + if (set_custom_baud_rate() < 0) { + UIM_ERR(" set_custom_baud_rate() failed"); + close(dev_fd); + + return -1; + } +#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 = line_discipline; + 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 { + /* UNINSTALL_N_TI_WL - When the Signal is received from KIM */ + /* closing UART fd */ + UIM_DBG(" Uninstalled N_TI_WL Line displine"); + close(dev_fd); + } + prev_st_state = st_state; + return 0; +} + +int remove_modules() +{ + int err = 0; + + /*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 "); + } + + 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); + rfkill_idx = id; + break; + } + } + if (id == 50) + return -1; + + 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; +} +/*****************************************************************************/ +int main(int argc, char *argv[]) +{ + int st_fd, err; + char buf[20] = { 0 }; + struct sigaction sa; + struct stat file_stat; + struct utsname name; + + struct pollfd p; + sigset_t sigs; + + UIM_START_FUNC(); + err = 0; + + /* Parse the user input */ + if (argc == 5) { + strcpy(uart_dev_name, argv[1]); + uart_baud_rate = atoi(argv[2]); + uart_flow_control = atoi(argv[3]); + line_discipline = atoi(argv[4]); + + /* Depending upon the baud rate value, differentiate + * the custom baud rate and default baud rate + */ + switch (uart_baud_rate) { + case 115200: + UIM_VER(" Baudrate 115200"); + break; + case 9600: + case 19200: + case 38400: + case 57600: + case 230400: + case 460800: + case 500000: + case 576000: + case 921600: + case 1000000: + case 1152000: + case 1500000: + case 2000000: + case 2500000: + case 3000000: + case 3500000: + case 3686400: + case 4000000: + cust_baud_rate = uart_baud_rate; + UIM_VER(" Baudrate %d", cust_baud_rate); + break; + default: + UIM_ERR(" Inavalid Baud Rate"); + break; + } + } else { + UIM_ERR(" Invalid arguments"); + UIM_ERR(" Usage: uim [Uart device] [Baud rate] \ + [Flow control] [Line discipline]"); + return -1; + } + + if (system("modprobe st_drv") != 0) { + UIM_ERR(" Error inserting st_drv module"); + return -1; + } else { + UIM_DBG(" Inserted st_drv module"); + } + + + if (change_rfkill_perms() < 0) { + /* possible error condition */ + UIM_ERR("rfkill not enabled in st_drv - \ + BT on from UI might fail\n"); + } + + st_fd = open("/dev/rfkill", O_RDONLY); + + p.fd = st_fd; + p.events = POLLERR | POLLHUP | POLLOUT | POLLIN; + + sigfillset(&sigs); + sigdelset(&sigs, SIGCHLD); + sigdelset(&sigs, SIGPIPE); + sigdelset(&sigs, SIGTERM); + sigdelset(&sigs, SIGINT); + sigdelset(&sigs, SIGHUP); + +RE_POLL: + while (!exiting) { + p.revents = 0; + err = ppoll(&p, 1, NULL, &sigs); + if (err < 0 && errno == EINTR) + continue; + if (err) + break; + } + if (!exiting) { + err = read(st_fd, &rf_event, sizeof(rf_event)); + UIM_DBG("rf_event: %d, %d, %d, %d, %d\n", rf_event.idx, + rf_event.type, rf_event.op , rf_event.hard, + rf_event.soft); + if ((rf_event.op == 2) && + (rf_event.idx == rfkill_idx)) { + if (rf_event.hard == 1) + st_state = UNINSTALL_N_TI_WL; + else + st_state = INSTALL_N_TI_WL; + + if (prev_st_state != st_state) + st_sig_handler(SIGUSR2); + } + goto RE_POLL; + } + + if (remove_modules() < 0) { + UIM_ERR(" Error removing modules "); + close(st_fd); + return -1; + } + + close(st_fd); + return 0; +} @@ -0,0 +1,140 @@ +/* + * 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 + +#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 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 + +/* 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 +/*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", __func__) +#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: Inside %s", __func__) +#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 + +/*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*/ +struct hci_command_hdr { + uint16_t opcode; /* OCF & OGF */ + uint8_t plen; +} __attribute__ ((packed)); + +/* HCI event header*/ +struct hci_event_hdr { + uint8_t evt; + uint8_t plen; +} __attribute__ ((packed)); + +/* HCI command complete event*/ +struct evt_cmd_complete { + uint8_t ncmd; + uint16_t opcode; +} __attribute__ ((packed)); + +/* HCI event status*/ +struct evt_cmd_status { + uint8_t status; + uint8_t ncmd; + uint16_t opcode; +} __attribute__ ((packed)); + +/* HCI Event structure to set the cusrom baud rate*/ +struct command_complete_t { + uint8_t uart_prefix; + struct hci_event_hdr hci_hdr; + struct evt_cmd_complete cmd_complete; + uint8_t status; + uint8_t data[16]; +} __attribute__ ((packed)); + +/* HCI Command structure to set the cusrom baud rate*/ +struct uim_speed_change_cmd { + uint8_t uart_prefix; + struct hci_command_hdr hci_hdr; + uint32_t speed; +} __attribute__ ((packed)); + +/* Signal received from KIM will install line discipline at first, + * the next signal received from KIM will un-install the + * line discipline*/ +enum { + /* expecting signal from KIM to setup uart fd for ST */ + INSTALL_N_TI_WL, + + /* expecting signal from KIM to close uart fd */ + UNINSTALL_N_TI_WL, +}; + +/* 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 */ diff --git a/uim.spec b/uim.spec new file mode 100644 index 0000000..ac1f0fd --- /dev/null +++ b/uim.spec @@ -0,0 +1,58 @@ +# +# Do not Edit! Generated by: +# spectacle version 0.15 +# +# >> macros +# << macros + +Name: uim +Summary: User Mode Initialization Manager +Version: 0.1 +Release: 1 +Group: System/Daemons +License: GPLv2+ +URL: http://dev.omapzoom.org +Source0: uim.tgz + +%description +User Mode Init manager - For TI shared transport drivers + + + + +%prep +%setup -q -n %{name} + +# >> setup +# << setup + +%build +# >> build pre +# << build pre + +%configure --disable-static +make %{?jobs:-j%jobs} + +# >> build post +# << build post +%install +rm -rf %{buildroot} +# >> install pre +# << install pre +%make_install + +# >> install post +# << install post + + + + + + +%files +%defattr(-,root,root,-) +%{_bindir}/uim +# >> files +# << files + + Binary files differdiff --git a/uim.yaml b/uim.yaml new file mode 100644 index 0000000..5561c78 --- /dev/null +++ b/uim.yaml @@ -0,0 +1,18 @@ +Name: uim +Summary: User Mode Initialization Manager +Version: 0.1 +Release: 1 +Group: System/Daemons +License: GPLv2+ +URL: http://dev.omapzoom.org +Description: | + User Mode Init manager - For TI shared transport drivers +Sources: + - uim.tgz +Configure: configure +Files: + - "%{_bindir}/uim" + + + + |