1 /****************************************************************************** 2 * 3 * Copyright (C) 2009-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 /****************************************************************************** 20 * 21 * Filename: userial_vendor.c 22 * 23 * Description: Contains vendor-specific userial functions 24 * 25 ******************************************************************************/ 26 27 #define LOG_TAG "bt_userial_vendor" 28 29 #include <utils/Log.h> 30 #include <termios.h> 31 #include <fcntl.h> 32 #include <errno.h> 33 #include <stdio.h> 34 #include "bt_vendor_brcm.h" 35 #include "userial.h" 36 #include "userial_vendor.h" 37 38 /****************************************************************************** 39 ** Constants & Macros 40 ******************************************************************************/ 41 42 #ifndef VNDUSERIAL_DBG 43 #define VNDUSERIAL_DBG FALSE 44 #endif 45 46 #if (VNDUSERIAL_DBG == TRUE) 47 #define VNDUSERIALDBG(param, ...) {ALOGD(param, ## __VA_ARGS__);} 48 #else 49 #define VNDUSERIALDBG(param, ...) {} 50 #endif 51 52 #define VND_PORT_NAME_MAXLEN 256 53 54 /****************************************************************************** 55 ** Local type definitions 56 ******************************************************************************/ 57 58 /* vendor serial control block */ 59 typedef struct 60 { 61 int fd; /* fd to Bluetooth device */ 62 struct termios termios; /* serial terminal of BT port */ 63 char port_name[VND_PORT_NAME_MAXLEN]; 64 } vnd_userial_cb_t; 65 66 /****************************************************************************** 67 ** Static variables 68 ******************************************************************************/ 69 70 static vnd_userial_cb_t vnd_userial; 71 72 /***************************************************************************** 73 ** Helper Functions 74 *****************************************************************************/ 75 76 /******************************************************************************* 77 ** 78 ** Function userial_to_tcio_baud 79 ** 80 ** Description helper function converts USERIAL baud rates into TCIO 81 ** conforming baud rates 82 ** 83 ** Returns TRUE/FALSE 84 ** 85 *******************************************************************************/ 86 uint8_t userial_to_tcio_baud(uint8_t cfg_baud, uint32_t *baud) 87 { 88 if (cfg_baud == USERIAL_BAUD_115200) 89 *baud = B115200; 90 else if (cfg_baud == USERIAL_BAUD_4M) 91 *baud = B4000000; 92 else if (cfg_baud == USERIAL_BAUD_3M) 93 *baud = B3000000; 94 else if (cfg_baud == USERIAL_BAUD_2M) 95 *baud = B2000000; 96 else if (cfg_baud == USERIAL_BAUD_1M) 97 *baud = B1000000; 98 else if (cfg_baud == USERIAL_BAUD_921600) 99 *baud = B921600; 100 else if (cfg_baud == USERIAL_BAUD_460800) 101 *baud = B460800; 102 else if (cfg_baud == USERIAL_BAUD_230400) 103 *baud = B230400; 104 else if (cfg_baud == USERIAL_BAUD_57600) 105 *baud = B57600; 106 else if (cfg_baud == USERIAL_BAUD_19200) 107 *baud = B19200; 108 else if (cfg_baud == USERIAL_BAUD_9600) 109 *baud = B9600; 110 else if (cfg_baud == USERIAL_BAUD_1200) 111 *baud = B1200; 112 else if (cfg_baud == USERIAL_BAUD_600) 113 *baud = B600; 114 else 115 { 116 ALOGE( "userial vendor open: unsupported baud idx %i", cfg_baud); 117 *baud = B115200; 118 return FALSE; 119 } 120 121 return TRUE; 122 } 123 124 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE) 125 /******************************************************************************* 126 ** 127 ** Function userial_ioctl_init_bt_wake 128 ** 129 ** Description helper function to set the open state of the bt_wake if ioctl 130 ** is used. it should not hurt in the rfkill case but it might 131 ** be better to compile it out. 132 ** 133 ** Returns none 134 ** 135 *******************************************************************************/ 136 void userial_ioctl_init_bt_wake(int fd) 137 { 138 uint32_t bt_wake_state; 139 140 /* assert BT_WAKE through ioctl */ 141 ioctl(fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL); 142 ioctl(fd, USERIAL_IOCTL_BT_WAKE_GET_ST, &bt_wake_state); 143 VNDUSERIALDBG("userial_ioctl_init_bt_wake read back BT_WAKE state=%i", \ 144 bt_wake_state); 145 } 146 #endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE) 147 148 149 /***************************************************************************** 150 ** Userial Vendor API Functions 151 *****************************************************************************/ 152 153 /******************************************************************************* 154 ** 155 ** Function userial_vendor_init 156 ** 157 ** Description Initialize userial vendor-specific control block 158 ** 159 ** Returns None 160 ** 161 *******************************************************************************/ 162 void userial_vendor_init(void) 163 { 164 vnd_userial.fd = -1; 165 snprintf(vnd_userial.port_name, VND_PORT_NAME_MAXLEN, "%s", \ 166 BLUETOOTH_UART_DEVICE_PORT); 167 } 168 169 /******************************************************************************* 170 ** 171 ** Function userial_vendor_open 172 ** 173 ** Description Open the serial port with the given configuration 174 ** 175 ** Returns device fd 176 ** 177 *******************************************************************************/ 178 int userial_vendor_open(tUSERIAL_CFG *p_cfg) 179 { 180 uint32_t baud; 181 uint8_t data_bits; 182 uint16_t parity; 183 uint8_t stop_bits; 184 185 vnd_userial.fd = -1; 186 187 if (!userial_to_tcio_baud(p_cfg->baud, &baud)) 188 { 189 return -1; 190 } 191 192 if(p_cfg->fmt & USERIAL_DATABITS_8) 193 data_bits = CS8; 194 else if(p_cfg->fmt & USERIAL_DATABITS_7) 195 data_bits = CS7; 196 else if(p_cfg->fmt & USERIAL_DATABITS_6) 197 data_bits = CS6; 198 else if(p_cfg->fmt & USERIAL_DATABITS_5) 199 data_bits = CS5; 200 else 201 { 202 ALOGE("userial vendor open: unsupported data bits"); 203 return -1; 204 } 205 206 if(p_cfg->fmt & USERIAL_PARITY_NONE) 207 parity = 0; 208 else if(p_cfg->fmt & USERIAL_PARITY_EVEN) 209 parity = PARENB; 210 else if(p_cfg->fmt & USERIAL_PARITY_ODD) 211 parity = (PARENB | PARODD); 212 else 213 { 214 ALOGE("userial vendor open: unsupported parity bit mode"); 215 return -1; 216 } 217 218 if(p_cfg->fmt & USERIAL_STOPBITS_1) 219 stop_bits = 0; 220 else if(p_cfg->fmt & USERIAL_STOPBITS_2) 221 stop_bits = CSTOPB; 222 else 223 { 224 ALOGE("userial vendor open: unsupported stop bits"); 225 return -1; 226 } 227 228 ALOGI("userial vendor open: opening %s", vnd_userial.port_name); 229 230 if ((vnd_userial.fd = open(vnd_userial.port_name, O_RDWR)) == -1) 231 { 232 ALOGE("userial vendor open: unable to open %s", vnd_userial.port_name); 233 return -1; 234 } 235 236 tcflush(vnd_userial.fd, TCIOFLUSH); 237 238 tcgetattr(vnd_userial.fd, &vnd_userial.termios); 239 cfmakeraw(&vnd_userial.termios); 240 vnd_userial.termios.c_cflag |= (CRTSCTS | stop_bits); 241 tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios); 242 tcflush(vnd_userial.fd, TCIOFLUSH); 243 244 tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios); 245 tcflush(vnd_userial.fd, TCIOFLUSH); 246 tcflush(vnd_userial.fd, TCIOFLUSH); 247 248 /* set input/output baudrate */ 249 cfsetospeed(&vnd_userial.termios, baud); 250 cfsetispeed(&vnd_userial.termios, baud); 251 tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios); 252 253 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE) 254 userial_ioctl_init_bt_wake(vnd_userial.fd); 255 #endif 256 257 ALOGI("device fd = %d open", vnd_userial.fd); 258 259 return vnd_userial.fd; 260 } 261 262 /******************************************************************************* 263 ** 264 ** Function userial_vendor_close 265 ** 266 ** Description Conduct vendor-specific close work 267 ** 268 ** Returns None 269 ** 270 *******************************************************************************/ 271 void userial_vendor_close(void) 272 { 273 int result; 274 275 if (vnd_userial.fd == -1) 276 return; 277 278 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE) 279 /* de-assert bt_wake BEFORE closing port */ 280 ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL); 281 #endif 282 283 ALOGI("device fd = %d close", vnd_userial.fd); 284 285 if ((result = close(vnd_userial.fd)) < 0) 286 ALOGE( "close(fd:%d) FAILED result:%d", vnd_userial.fd, result); 287 288 vnd_userial.fd = -1; 289 } 290 291 /******************************************************************************* 292 ** 293 ** Function userial_vendor_set_baud 294 ** 295 ** Description Set new baud rate 296 ** 297 ** Returns None 298 ** 299 *******************************************************************************/ 300 void userial_vendor_set_baud(uint8_t userial_baud) 301 { 302 uint32_t tcio_baud; 303 304 userial_to_tcio_baud(userial_baud, &tcio_baud); 305 306 cfsetospeed(&vnd_userial.termios, tcio_baud); 307 cfsetispeed(&vnd_userial.termios, tcio_baud); 308 tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios); 309 } 310 311 /******************************************************************************* 312 ** 313 ** Function userial_vendor_ioctl 314 ** 315 ** Description ioctl inteface 316 ** 317 ** Returns None 318 ** 319 *******************************************************************************/ 320 void userial_vendor_ioctl(userial_vendor_ioctl_op_t op, void *p_data) 321 { 322 switch(op) 323 { 324 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE) 325 case USERIAL_OP_ASSERT_BT_WAKE: 326 VNDUSERIALDBG("## userial_vendor_ioctl: Asserting BT_Wake ##"); 327 ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL); 328 break; 329 330 case USERIAL_OP_DEASSERT_BT_WAKE: 331 VNDUSERIALDBG("## userial_vendor_ioctl: De-asserting BT_Wake ##"); 332 ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL); 333 break; 334 335 case USERIAL_OP_GET_BT_WAKE_STATE: 336 ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_GET_ST, p_data); 337 break; 338 #endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE) 339 340 default: 341 break; 342 } 343 } 344 345 /******************************************************************************* 346 ** 347 ** Function userial_set_port 348 ** 349 ** Description Configure UART port name 350 ** 351 ** Returns 0 : Success 352 ** Otherwise : Fail 353 ** 354 *******************************************************************************/ 355 int userial_set_port(char *p_conf_name, char *p_conf_value, int param) 356 { 357 strcpy(vnd_userial.port_name, p_conf_value); 358 359 return 0; 360 } 361 362