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 <log/log.h>
30 #include <termios.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <stdio.h>
34 #include <unistd.h>
35 #include "bt_vendor_qcom.h"
36 #include "hci_uart.h"
37 #include <string.h>
38
39 /******************************************************************************
40 ** Constants & Macros
41 ******************************************************************************/
42
43 #ifndef VNDUSERIAL_DBG
44 #define VNDUSERIAL_DBG TRUE
45 #endif
46
47 #if (VNDUSERIAL_DBG == TRUE)
48 #define VNDUSERIALDBG(param, ...) {ALOGI(param, ## __VA_ARGS__);}
49 #else
50 #define VNDUSERIALDBG(param, ...) {}
51 #endif
52
53 #define RESERVED(p) if(p) ALOGI( "%s: reserved param", __FUNCTION__);
54
55 /******************************************************************************
56 ** Global variables
57 ******************************************************************************/
58 vnd_userial_cb_t vnd_userial;
59
60 /*****************************************************************************
61 ** Functions
62 *****************************************************************************/
63
64 /*******************************************************************************
65 **
66 ** Function userial_to_tcio_baud
67 **
68 ** Description helper function converts USERIAL baud rates into TCIO
69 ** conforming baud rates
70 **
71 ** Returns TRUE/FALSE
72 **
73 *******************************************************************************/
userial_to_tcio_baud(uint8_t cfg_baud,uint32_t * baud)74 uint8_t userial_to_tcio_baud(uint8_t cfg_baud, uint32_t *baud)
75 {
76 if (cfg_baud == USERIAL_BAUD_115200)
77 *baud = B115200;
78 else if (cfg_baud == USERIAL_BAUD_4M)
79 *baud = B4000000;
80 else if (cfg_baud == USERIAL_BAUD_3M)
81 *baud = B3000000;
82 else if (cfg_baud == USERIAL_BAUD_2M)
83 *baud = B2000000;
84 else if (cfg_baud == USERIAL_BAUD_1M)
85 *baud = B1000000;
86 else if (cfg_baud == USERIAL_BAUD_921600)
87 *baud = B921600;
88 else if (cfg_baud == USERIAL_BAUD_460800)
89 *baud = B460800;
90 else if (cfg_baud == USERIAL_BAUD_230400)
91 *baud = B230400;
92 else if (cfg_baud == USERIAL_BAUD_57600)
93 *baud = B57600;
94 else if (cfg_baud == USERIAL_BAUD_19200)
95 *baud = B19200;
96 else if (cfg_baud == USERIAL_BAUD_9600)
97 *baud = B9600;
98 else if (cfg_baud == USERIAL_BAUD_1200)
99 *baud = B1200;
100 else if (cfg_baud == USERIAL_BAUD_600)
101 *baud = B600;
102 else
103 {
104 ALOGE( "userial vendor open: unsupported baud idx %i", cfg_baud);
105 *baud = B115200;
106 return FALSE;
107 }
108
109 return TRUE;
110 }
111
112 /*******************************************************************************
113 **
114 ** Function userial_to_baud_tcio
115 **
116 ** Description helper function converts TCIO baud rate into integer
117 **
118 ** Returns uint32_t
119 **
120 *******************************************************************************/
userial_tcio_baud_to_int(uint32_t baud)121 int userial_tcio_baud_to_int(uint32_t baud)
122 {
123 int baud_rate =0;
124
125 switch (baud)
126 {
127 case B600:
128 baud_rate = 600;
129 break;
130 case B1200:
131 baud_rate = 1200;
132 break;
133 case B9600:
134 baud_rate = 9600;
135 break;
136 case B19200:
137 baud_rate = 19200;
138 break;
139 case B57600:
140 baud_rate = 57600;
141 break;
142 case B115200:
143 baud_rate = 115200;
144 break;
145 case B230400:
146 baud_rate = 230400;
147 break;
148 case B460800:
149 baud_rate = 460800;
150 break;
151 case B921600:
152 baud_rate = 921600;
153 break;
154 case B1000000:
155 baud_rate = 1000000;
156 break;
157 case B2000000:
158 baud_rate = 2000000;
159 break;
160 case B3000000:
161 baud_rate = 3000000;
162 break;
163 case B4000000:
164 baud_rate = 4000000;
165 break;
166 default:
167 ALOGE( "%s: unsupported baud %d", __FUNCTION__, baud);
168 break;
169 }
170
171 ALOGI( "%s: Current Baudrate = %d bps", __FUNCTION__, baud_rate);
172 return baud_rate;
173 }
174
175
176 #if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
177 /*******************************************************************************
178 **
179 ** Function userial_ioctl_init_bt_wake
180 **
181 ** Description helper function to set the open state of the bt_wake if ioctl
182 ** is used. it should not hurt in the rfkill case but it might
183 ** be better to compile it out.
184 **
185 ** Returns none
186 **
187 *******************************************************************************/
userial_ioctl_init_bt_wake(int fd)188 void userial_ioctl_init_bt_wake(int fd)
189 {
190 uint32_t bt_wake_state;
191
192 /* assert BT_WAKE through ioctl */
193 ioctl(fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
194 ioctl(fd, USERIAL_IOCTL_BT_WAKE_GET_ST, &bt_wake_state);
195 VNDUSERIALDBG("userial_ioctl_init_bt_wake read back BT_WAKE state=%i", \
196 bt_wake_state);
197 }
198 #endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
199
200
201 /*****************************************************************************
202 ** Userial Vendor API Functions
203 *****************************************************************************/
204
205 /*******************************************************************************
206 **
207 ** Function userial_vendor_init
208 **
209 ** Description Initialize userial vendor-specific control block
210 **
211 ** Returns None
212 **
213 *******************************************************************************/
userial_vendor_init(void)214 void userial_vendor_init(void)
215 {
216 vnd_userial.fd = -1;
217 snprintf(vnd_userial.port_name, VND_PORT_NAME_MAXLEN, "%s", BT_HS_UART_DEVICE);
218 }
219
220 /*******************************************************************************
221 **
222 ** Function userial_vendor_open
223 **
224 ** Description Open the serial port with the given configuration
225 **
226 ** Returns device fd
227 **
228 *******************************************************************************/
userial_vendor_open(tUSERIAL_CFG * p_cfg)229 int userial_vendor_open(tUSERIAL_CFG *p_cfg)
230 {
231 uint32_t baud;
232 uint8_t data_bits;
233 uint16_t parity;
234 uint8_t stop_bits;
235
236 vnd_userial.fd = -1;
237
238 if (!userial_to_tcio_baud(p_cfg->baud, &baud))
239 {
240 return -1;
241 }
242
243 if(p_cfg->fmt & USERIAL_DATABITS_8)
244 data_bits = CS8;
245 else if(p_cfg->fmt & USERIAL_DATABITS_7)
246 data_bits = CS7;
247 else if(p_cfg->fmt & USERIAL_DATABITS_6)
248 data_bits = CS6;
249 else if(p_cfg->fmt & USERIAL_DATABITS_5)
250 data_bits = CS5;
251 else
252 {
253 ALOGE("userial vendor open: unsupported data bits");
254 return -1;
255 }
256
257 if(p_cfg->fmt & USERIAL_PARITY_NONE)
258 parity = 0;
259 else if(p_cfg->fmt & USERIAL_PARITY_EVEN)
260 parity = PARENB;
261 else if(p_cfg->fmt & USERIAL_PARITY_ODD)
262 parity = (PARENB | PARODD);
263 else
264 {
265 ALOGE("userial vendor open: unsupported parity bit mode");
266 return -1;
267 }
268
269 if(p_cfg->fmt & USERIAL_STOPBITS_1)
270 stop_bits = 0;
271 else if(p_cfg->fmt & USERIAL_STOPBITS_2)
272 stop_bits = CSTOPB;
273 else
274 {
275 ALOGE("userial vendor open: unsupported stop bits");
276 return -1;
277 }
278
279 ALOGI("userial vendor open: opening %s", vnd_userial.port_name);
280
281 if ((vnd_userial.fd = open(vnd_userial.port_name, O_RDWR|O_NOCTTY)) == -1)
282 {
283 ALOGE("userial vendor open: unable to open %s: %s(%d)", vnd_userial.port_name,
284 strerror(errno), errno);
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