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