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 */
59typedef 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
70static 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*******************************************************************************/
86uint8_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*******************************************************************************/
136void userial_ioctl_init_bt_wake(int fd)
137{
138    uint32_t bt_wake_state;
139
140#if (BT_WAKE_USERIAL_LDISC==TRUE)
141    int ldisc = N_BRCM_HCI; /* brcm sleep mode support line discipline */
142
143    /* attempt to load enable discipline driver */
144    if (ioctl(vnd_userial.fd, TIOCSETD, &ldisc) < 0)
145    {
146        VNDUSERIALDBG("USERIAL_Open():fd %d, TIOCSETD failed: error %d for ldisc: %d",
147                      fd, errno, ldisc);
148    }
149#endif
150
151
152
153    /* assert BT_WAKE through ioctl */
154    ioctl(fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
155    ioctl(fd, USERIAL_IOCTL_BT_WAKE_GET_ST, &bt_wake_state);
156    VNDUSERIALDBG("userial_ioctl_init_bt_wake read back BT_WAKE state=%i", \
157               bt_wake_state);
158}
159#endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
160
161
162/*****************************************************************************
163**   Userial Vendor API Functions
164*****************************************************************************/
165
166/*******************************************************************************
167**
168** Function        userial_vendor_init
169**
170** Description     Initialize userial vendor-specific control block
171**
172** Returns         None
173**
174*******************************************************************************/
175void userial_vendor_init(void)
176{
177    vnd_userial.fd = -1;
178    snprintf(vnd_userial.port_name, VND_PORT_NAME_MAXLEN, "%s", \
179            BLUETOOTH_UART_DEVICE_PORT);
180}
181
182/*******************************************************************************
183**
184** Function        userial_vendor_open
185**
186** Description     Open the serial port with the given configuration
187**
188** Returns         device fd
189**
190*******************************************************************************/
191int userial_vendor_open(tUSERIAL_CFG *p_cfg)
192{
193    uint32_t baud;
194    uint8_t data_bits;
195    uint16_t parity;
196    uint8_t stop_bits;
197
198    vnd_userial.fd = -1;
199
200    if (!userial_to_tcio_baud(p_cfg->baud, &baud))
201    {
202        return -1;
203    }
204
205    if(p_cfg->fmt & USERIAL_DATABITS_8)
206        data_bits = CS8;
207    else if(p_cfg->fmt & USERIAL_DATABITS_7)
208        data_bits = CS7;
209    else if(p_cfg->fmt & USERIAL_DATABITS_6)
210        data_bits = CS6;
211    else if(p_cfg->fmt & USERIAL_DATABITS_5)
212        data_bits = CS5;
213    else
214    {
215        ALOGE("userial vendor open: unsupported data bits");
216        return -1;
217    }
218
219    if(p_cfg->fmt & USERIAL_PARITY_NONE)
220        parity = 0;
221    else if(p_cfg->fmt & USERIAL_PARITY_EVEN)
222        parity = PARENB;
223    else if(p_cfg->fmt & USERIAL_PARITY_ODD)
224        parity = (PARENB | PARODD);
225    else
226    {
227        ALOGE("userial vendor open: unsupported parity bit mode");
228        return -1;
229    }
230
231    if(p_cfg->fmt & USERIAL_STOPBITS_1)
232        stop_bits = 0;
233    else if(p_cfg->fmt & USERIAL_STOPBITS_2)
234        stop_bits = CSTOPB;
235    else
236    {
237        ALOGE("userial vendor open: unsupported stop bits");
238        return -1;
239    }
240
241    ALOGI("userial vendor open: opening %s", vnd_userial.port_name);
242
243    if ((vnd_userial.fd = open(vnd_userial.port_name, O_RDWR)) == -1)
244    {
245        ALOGE("userial vendor open: unable to open %s", vnd_userial.port_name);
246        return -1;
247    }
248
249    tcflush(vnd_userial.fd, TCIOFLUSH);
250
251    tcgetattr(vnd_userial.fd, &vnd_userial.termios);
252    cfmakeraw(&vnd_userial.termios);
253    vnd_userial.termios.c_cflag |= (CRTSCTS | stop_bits);
254    tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
255    tcflush(vnd_userial.fd, TCIOFLUSH);
256
257    tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
258    tcflush(vnd_userial.fd, TCIOFLUSH);
259    tcflush(vnd_userial.fd, TCIOFLUSH);
260
261    /* set input/output baudrate */
262    cfsetospeed(&vnd_userial.termios, baud);
263    cfsetispeed(&vnd_userial.termios, baud);
264    tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
265
266#if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
267    userial_ioctl_init_bt_wake(vnd_userial.fd);
268#endif
269
270    ALOGI("device fd = %d open", vnd_userial.fd);
271
272    return vnd_userial.fd;
273}
274
275/*******************************************************************************
276**
277** Function        userial_vendor_close
278**
279** Description     Conduct vendor-specific close work
280**
281** Returns         None
282**
283*******************************************************************************/
284void userial_vendor_close(void)
285{
286    int result;
287
288    if (vnd_userial.fd == -1)
289        return;
290
291#if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
292    /* de-assert bt_wake BEFORE closing port */
293    ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL);
294#endif
295
296    ALOGI("device fd = %d close", vnd_userial.fd);
297    // flush Tx before close to make sure no chars in buffer
298    tcflush(vnd_userial.fd, TCIOFLUSH);
299    if ((result = close(vnd_userial.fd)) < 0)
300        ALOGE( "close(fd:%d) FAILED result:%d", vnd_userial.fd, result);
301
302    vnd_userial.fd = -1;
303}
304
305/*******************************************************************************
306**
307** Function        userial_vendor_set_baud
308**
309** Description     Set new baud rate
310**
311** Returns         None
312**
313*******************************************************************************/
314void userial_vendor_set_baud(uint8_t userial_baud)
315{
316    uint32_t tcio_baud;
317
318    userial_to_tcio_baud(userial_baud, &tcio_baud);
319
320    cfsetospeed(&vnd_userial.termios, tcio_baud);
321    cfsetispeed(&vnd_userial.termios, tcio_baud);
322    tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
323}
324
325/*******************************************************************************
326**
327** Function        userial_vendor_ioctl
328**
329** Description     ioctl inteface
330**
331** Returns         None
332**
333*******************************************************************************/
334void userial_vendor_ioctl(userial_vendor_ioctl_op_t op, void *p_data)
335{
336    switch(op)
337    {
338#if (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
339        case USERIAL_OP_ASSERT_BT_WAKE:
340            VNDUSERIALDBG("## userial_vendor_ioctl: Asserting BT_Wake ##");
341            ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
342            break;
343
344        case USERIAL_OP_DEASSERT_BT_WAKE:
345            VNDUSERIALDBG("## userial_vendor_ioctl: De-asserting BT_Wake ##");
346            ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL);
347            break;
348
349        case USERIAL_OP_GET_BT_WAKE_STATE:
350            ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_GET_ST, p_data);
351            break;
352#endif  //  (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
353
354        default:
355            break;
356    }
357}
358
359/*******************************************************************************
360**
361** Function        userial_set_port
362**
363** Description     Configure UART port name
364**
365** Returns         0 : Success
366**                 Otherwise : Fail
367**
368*******************************************************************************/
369int userial_set_port(char *p_conf_name, char *p_conf_value, int param)
370{
371    strcpy(vnd_userial.port_name, p_conf_value);
372
373    return 0;
374}
375
376