1 /*
2 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 * Copyright 2012 The Android Open Source Project
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
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_vendor"
28
29 #include <termios.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <log/log.h>
36 #include "bt_vendor_qcom.h"
37 #include "hci_uart.h"
38 #include <string.h>
39
40 /******************************************************************************
41 ** Constants & Macros
42 ******************************************************************************/
43
44 #ifndef VNDUSERIAL_DBG
45 #define VNDUSERIAL_DBG TRUE
46 #endif
47
48 #if (VNDUSERIAL_DBG == TRUE)
49 #define VNDUSERIALDBG(param, ...) {ALOGI(param, ## __VA_ARGS__);}
50 #else
51 #define VNDUSERIALDBG(param, ...) {}
52 #endif
53
54 #define RESERVED(p) if(p) ALOGI( "%s: reserved param", __FUNCTION__);
55
56 /******************************************************************************
57 ** Global variables
58 ******************************************************************************/
59 vnd_userial_cb_t vnd_userial;
60
61 /*****************************************************************************
62 ** Functions
63 *****************************************************************************/
64
65 /*******************************************************************************
66 **
67 ** Function userial_to_tcio_baud
68 **
69 ** Description helper function converts USERIAL baud rates into TCIO
70 ** conforming baud rates
71 **
72 ** Returns TRUE/FALSE
73 **
74 *******************************************************************************/
userial_to_tcio_baud(uint8_t cfg_baud,uint32_t * baud)75 uint8_t userial_to_tcio_baud(uint8_t cfg_baud, uint32_t *baud)
76 {
77 if (cfg_baud == USERIAL_BAUD_115200)
78 *baud = B115200;
79 else if (cfg_baud == USERIAL_BAUD_4M)
80 *baud = B4000000;
81 else if (cfg_baud == USERIAL_BAUD_3M)
82 *baud = B3000000;
83 else if (cfg_baud == USERIAL_BAUD_2M)
84 *baud = B2000000;
85 else if (cfg_baud == USERIAL_BAUD_1M)
86 *baud = B1000000;
87 else if (cfg_baud == USERIAL_BAUD_921600)
88 *baud = B921600;
89 else if (cfg_baud == USERIAL_BAUD_460800)
90 *baud = B460800;
91 else if (cfg_baud == USERIAL_BAUD_230400)
92 *baud = B230400;
93 else if (cfg_baud == USERIAL_BAUD_57600)
94 *baud = B57600;
95 else if (cfg_baud == USERIAL_BAUD_19200)
96 *baud = B19200;
97 else if (cfg_baud == USERIAL_BAUD_9600)
98 *baud = B9600;
99 else if (cfg_baud == USERIAL_BAUD_1200)
100 *baud = B1200;
101 else if (cfg_baud == USERIAL_BAUD_600)
102 *baud = B600;
103 else
104 {
105 ALOGE( "userial vendor open: unsupported baud idx %i", cfg_baud);
106 *baud = B115200;
107 return FALSE;
108 }
109
110 return TRUE;
111 }
112
113 /*******************************************************************************
114 **
115 ** Function userial_to_baud_tcio
116 **
117 ** Description helper function converts TCIO baud rate into integer
118 **
119 ** Returns uint32_t
120 **
121 *******************************************************************************/
userial_tcio_baud_to_int(uint32_t baud)122 int userial_tcio_baud_to_int(uint32_t baud)
123 {
124 int baud_rate =0;
125
126 switch (baud)
127 {
128 case B600:
129 baud_rate = 600;
130 break;
131 case B1200:
132 baud_rate = 1200;
133 break;
134 case B9600:
135 baud_rate = 9600;
136 break;
137 case B19200:
138 baud_rate = 19200;
139 break;
140 case B57600:
141 baud_rate = 57600;
142 break;
143 case B115200:
144 baud_rate = 115200;
145 break;
146 case B230400:
147 baud_rate = 230400;
148 break;
149 case B460800:
150 baud_rate = 460800;
151 break;
152 case B921600:
153 baud_rate = 921600;
154 break;
155 case B1000000:
156 baud_rate = 1000000;
157 break;
158 case B2000000:
159 baud_rate = 2000000;
160 break;
161 case B3000000:
162 baud_rate = 3000000;
163 break;
164 case B4000000:
165 baud_rate = 4000000;
166 break;
167 default:
168 ALOGE( "%s: unsupported baud %d", __FUNCTION__, baud);
169 break;
170 }
171
172 ALOGI( "%s: Current Baudrate = %d bps", __FUNCTION__, baud_rate);
173 return baud_rate;
174 }
175
176
177 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
178 /*******************************************************************************
179 **
180 ** Function userial_ioctl_init_bt_wake
181 **
182 ** Description helper function to set the open state of the bt_wake if ioctl
183 ** is used. it should not hurt in the rfkill case but it might
184 ** be better to compile it out.
185 **
186 ** Returns none
187 **
188 *******************************************************************************/
userial_ioctl_init_bt_wake(int fd)189 void userial_ioctl_init_bt_wake(int fd)
190 {
191 uint32_t bt_wake_state;
192
193 /* assert BT_WAKE through ioctl */
194 ioctl(fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
195 ioctl(fd, USERIAL_IOCTL_BT_WAKE_GET_ST, &bt_wake_state);
196 VNDUSERIALDBG("userial_ioctl_init_bt_wake read back BT_WAKE state=%i", \
197 bt_wake_state);
198 }
199 #endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
200
201
202 /*****************************************************************************
203 ** Userial Vendor API Functions
204 *****************************************************************************/
205
206 /*******************************************************************************
207 **
208 ** Function userial_vendor_init
209 **
210 ** Description Initialize userial vendor-specific control block
211 **
212 ** Returns None
213 **
214 *******************************************************************************/
userial_vendor_init(void)215 void userial_vendor_init(void)
216 {
217 vnd_userial.fd = -1;
218 snprintf(vnd_userial.port_name, VND_PORT_NAME_MAXLEN, "%s", BT_HS_UART_DEVICE);
219 }
220
221 /*******************************************************************************
222 **
223 ** Function userial_vendor_open
224 **
225 ** Description Open the serial port with the given configuration
226 **
227 ** Returns device fd
228 **
229 *******************************************************************************/
userial_vendor_open(tUSERIAL_CFG * p_cfg)230 int userial_vendor_open(tUSERIAL_CFG *p_cfg)
231 {
232 uint32_t baud;
233 uint8_t data_bits;
234 uint16_t parity;
235 uint8_t stop_bits;
236
237 vnd_userial.fd = -1;
238
239 if (!userial_to_tcio_baud(p_cfg->baud, &baud))
240 {
241 return -1;
242 }
243
244 if(p_cfg->fmt & USERIAL_DATABITS_8)
245 data_bits = CS8;
246 else if(p_cfg->fmt & USERIAL_DATABITS_7)
247 data_bits = CS7;
248 else if(p_cfg->fmt & USERIAL_DATABITS_6)
249 data_bits = CS6;
250 else if(p_cfg->fmt & USERIAL_DATABITS_5)
251 data_bits = CS5;
252 else
253 {
254 ALOGE("userial vendor open: unsupported data bits");
255 return -1;
256 }
257
258 if(p_cfg->fmt & USERIAL_PARITY_NONE)
259 parity = 0;
260 else if(p_cfg->fmt & USERIAL_PARITY_EVEN)
261 parity = PARENB;
262 else if(p_cfg->fmt & USERIAL_PARITY_ODD)
263 parity = (PARENB | PARODD);
264 else
265 {
266 ALOGE("userial vendor open: unsupported parity bit mode");
267 return -1;
268 }
269
270 if(p_cfg->fmt & USERIAL_STOPBITS_1)
271 stop_bits = 0;
272 else if(p_cfg->fmt & USERIAL_STOPBITS_2)
273 stop_bits = CSTOPB;
274 else
275 {
276 ALOGE("userial vendor open: unsupported stop bits");
277 return -1;
278 }
279
280 ALOGI("userial vendor open: opening %s", vnd_userial.port_name);
281
282 if ((vnd_userial.fd = open(vnd_userial.port_name, O_RDWR|O_NOCTTY)) == -1)
283 {
284 ALOGE("userial vendor open: unable to open %s", vnd_userial.port_name);
285 return -1;
286 }
287
288 tcflush(vnd_userial.fd, TCIOFLUSH);
289
290 tcgetattr(vnd_userial.fd, &vnd_userial.termios);
291 cfmakeraw(&vnd_userial.termios);
292
293 /* Set UART Control Modes */
294 vnd_userial.termios.c_cflag |= CLOCAL;
295 vnd_userial.termios.c_cflag |= (CRTSCTS | stop_bits);
296
297 tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
298
299 /* set input/output baudrate */
300 cfsetospeed(&vnd_userial.termios, baud);
301 cfsetispeed(&vnd_userial.termios, baud);
302 tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
303
304 tcflush(vnd_userial.fd, TCIOFLUSH);
305
306 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
307 userial_ioctl_init_bt_wake(vnd_userial.fd);
308 #endif
309
310 ALOGI("device fd = %d open", vnd_userial.fd);
311
312 return vnd_userial.fd;
313 }
314
315 /*******************************************************************************
316 **
317 ** Function userial_vendor_close
318 **
319 ** Description Conduct vendor-specific close work
320 **
321 ** Returns None
322 **
323 *******************************************************************************/
userial_vendor_close(void)324 void userial_vendor_close(void)
325 {
326 int result;
327
328 if (vnd_userial.fd == -1)
329 return;
330
331 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
332 /* de-assert bt_wake BEFORE closing port */
333 ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL);
334 #endif
335
336 ALOGI("device fd = %d close", vnd_userial.fd);
337
338 if ((result = close(vnd_userial.fd)) < 0)
339 ALOGE( "close(fd:%d) FAILED result:%d", vnd_userial.fd, result);
340
341 vnd_userial.fd = -1;
342 }
343
344 /*******************************************************************************
345 **
346 ** Function userial_vendor_set_baud
347 **
348 ** Description Set new baud rate
349 **
350 ** Returns None
351 **
352 *******************************************************************************/
userial_vendor_set_baud(uint8_t userial_baud)353 void userial_vendor_set_baud(uint8_t userial_baud)
354 {
355 uint32_t tcio_baud;
356
357 VNDUSERIALDBG("## userial_vendor_set_baud: %d", userial_baud);
358
359 userial_to_tcio_baud(userial_baud, &tcio_baud);
360
361 cfsetospeed(&vnd_userial.termios, tcio_baud);
362 cfsetispeed(&vnd_userial.termios, tcio_baud);
363 tcsetattr(vnd_userial.fd, TCSADRAIN, &vnd_userial.termios); /* don't change speed until last write done */
364 // tcflush(vnd_userial.fd, TCIOFLUSH);
365 }
366
367 /*******************************************************************************
368 **
369 ** Function userial_vendor_get_baud
370 **
371 ** Description Get current baud rate
372 **
373 ** Returns int
374 **
375 *******************************************************************************/
userial_vendor_get_baud(void)376 int userial_vendor_get_baud(void)
377 {
378 if (vnd_userial.fd == -1)
379 {
380 ALOGE( "%s: uart port(%s) has not been opened", __FUNCTION__, BT_HS_UART_DEVICE );
381 return -1;
382 }
383
384 return userial_tcio_baud_to_int(cfgetispeed(&vnd_userial.termios));
385 }
386
387 /*******************************************************************************
388 **
389 ** Function userial_vendor_ioctl
390 **
391 ** Description ioctl inteface
392 **
393 ** Returns None
394 **
395 *******************************************************************************/
userial_vendor_ioctl(userial_vendor_ioctl_op_t op,int * p_data)396 int userial_vendor_ioctl(userial_vendor_ioctl_op_t op, int *p_data)
397 {
398 int err = -1;
399
400 switch(op)
401 {
402 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
403 case USERIAL_OP_ASSERT_BT_WAKE:
404 VNDUSERIALDBG("## userial_vendor_ioctl: Asserting BT_Wake ##");
405 err = ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
406 break;
407
408 case USERIAL_OP_DEASSERT_BT_WAKE:
409 VNDUSERIALDBG("## userial_vendor_ioctl: De-asserting BT_Wake ##");
410 err = ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL);
411 break;
412
413 case USERIAL_OP_GET_BT_WAKE_STATE:
414 err = ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_GET_ST, p_data);
415 break;
416 #endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
417 case USERIAL_OP_FLOW_ON:
418 ALOGI("## userial_vendor_ioctl: UART Flow On ");
419 *p_data |=TIOCM_RTS;
420 err = ioctl(vnd_userial.fd, TIOCMSET, p_data);
421 break;
422
423 case USERIAL_OP_FLOW_OFF:
424 ALOGI("## userial_vendor_ioctl: UART Flow Off ");
425 ioctl(vnd_userial.fd, TIOCMGET, p_data);
426 *p_data &= ~TIOCM_RTS;
427 err = ioctl(vnd_userial.fd, TIOCMSET, p_data);
428 break;
429
430 default:
431 break;
432 }
433
434 return err;
435 }
436
437 /*******************************************************************************
438 **
439 ** Function userial_set_port
440 **
441 ** Description Configure UART port name
442 **
443 ** Returns 0 : Success
444 ** Otherwise : Fail
445 **
446 *******************************************************************************/
userial_set_port(char * p_conf_name,char * p_conf_value,int param)447 int userial_set_port(char *p_conf_name, char *p_conf_value, int param)
448 {
449 RESERVED(p_conf_name);
450 RESERVED(param);
451 strlcpy(vnd_userial.port_name, p_conf_value, VND_PORT_NAME_MAXLEN);
452
453 return 0;
454 }
455
456 /*******************************************************************************
457 **
458 ** Function read_hci_event
459 **
460 ** Description Read HCI event during vendor initialization
461 **
462 ** Returns int: size to read
463 **
464 *******************************************************************************/
read_hci_event(int fd,unsigned char * buf,int size)465 int read_hci_event(int fd, unsigned char* buf, int size)
466 {
467 int remain, r;
468 int count = 0;
469
470 if (size <= 0) {
471 ALOGE("Invalid size arguement!");
472 return -1;
473 }
474
475 ALOGI("%s: Wait for Command Compete Event from SOC", __FUNCTION__);
476
477 /* The first byte identifies the packet type. For HCI event packets, it
478 * should be 0x04, so we read until we get to the 0x04. */
479 while (1) {
480 r = read(fd, buf, 1);
481 if (r <= 0)
482 return -1;
483 if (buf[0] == 0x04)
484 break;
485 }
486 count++;
487
488 /* The next two bytes are the event code and parameter total length. */
489 while (count < 3) {
490 r = read(fd, buf + count, 3 - count);
491 if (r <= 0)
492 return -1;
493 count += r;
494 }
495
496 /* Now we read the parameters. */
497 if (buf[2] < (size - 3))
498 remain = buf[2];
499 else
500 remain = size - 3;
501
502 while ((count - 3) < remain) {
503 r = read(fd, buf + count, remain - (count - 3));
504 if (r <= 0)
505 return -1;
506 count += r;
507 }
508 return count;
509 }
510
userial_clock_operation(int fd,int cmd)511 int userial_clock_operation(int fd, int cmd)
512 {
513 int ret = 0;
514
515 switch (cmd)
516 {
517 case USERIAL_OP_CLK_ON:
518 case USERIAL_OP_CLK_OFF:
519 ioctl(fd, cmd);
520 break;
521 case USERIAL_OP_CLK_STATE:
522 ret = ioctl(fd, cmd);
523 break;
524 }
525
526 return ret;
527 }
528