• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <termios.h>
30 #include <fcntl.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <utils/Log.h>
36 #include "bt_vendor_brcm.h"
37 #include "userial.h"
38 #include "userial_vendor.h"
39 
40 /******************************************************************************
41 **  Constants & Macros
42 ******************************************************************************/
43 
44 #ifndef VNDUSERIAL_DBG
45 #define VNDUSERIAL_DBG FALSE
46 #endif
47 
48 #if (VNDUSERIAL_DBG == TRUE)
49 #define VNDUSERIALDBG(param, ...)                                              \
50     {                                                                          \
51         HILOGD(param, ##__VA_ARGS__);                                          \
52     }
53 #else
54 #define VNDUSERIALDBG(param, ...)                                              \
55     {                                                                          \
56         HILOGD(param, ##__VA_ARGS__);                                          \
57     }
58 #endif
59 
60 #define VND_PORT_NAME_MAXLEN 256
61 
62 /******************************************************************************
63 **  Local type definitions
64 ******************************************************************************/
65 
66 /* vendor serial control block */
67 typedef struct {
68     int fd;                 /* fd to Bluetooth device */
69     struct termios termios; /* serial terminal of BT port */
70     char port_name[VND_PORT_NAME_MAXLEN];
71 } vnd_userial_cb_t;
72 
73 /******************************************************************************
74 **  Static variables
75 ******************************************************************************/
76 
77 static vnd_userial_cb_t vnd_userial;
78 
79 /*****************************************************************************
80 **   Helper Functions
81 *****************************************************************************/
82 
83 /*******************************************************************************
84 **
85 ** Function        userial_to_tcio_baud
86 **
87 ** Description     helper function converts USERIAL baud rates into TCIO
88 **                  conforming baud rates
89 **
90 ** Returns         TRUE/FALSE
91 **
92 *******************************************************************************/
userial_to_tcio_baud(uint8_t cfg_baud,uint32_t * baud)93 uint8_t userial_to_tcio_baud(uint8_t cfg_baud, uint32_t *baud)
94 {
95     if (cfg_baud == USERIAL_BAUD_115200) {
96         *baud = B115200;
97     } else if (cfg_baud == USERIAL_BAUD_4M) {
98         *baud = B4000000;
99     } else if (cfg_baud == USERIAL_BAUD_3M) {
100         *baud = B3000000;
101     } else if (cfg_baud == USERIAL_BAUD_2M) {
102         *baud = B2000000;
103     } else if (cfg_baud == USERIAL_BAUD_1_5M) {
104         *baud = B1500000;
105     } else if (cfg_baud == USERIAL_BAUD_1M) {
106         *baud = B1000000;
107     } else if (cfg_baud == USERIAL_BAUD_921600) {
108         *baud = B921600;
109     } else if (cfg_baud == USERIAL_BAUD_460800) {
110         *baud = B460800;
111     } else if (cfg_baud == USERIAL_BAUD_230400) {
112         *baud = B230400;
113     } else if (cfg_baud == USERIAL_BAUD_57600) {
114         *baud = B57600;
115     } else if (cfg_baud == USERIAL_BAUD_19200) {
116         *baud = B19200;
117     } else if (cfg_baud == USERIAL_BAUD_9600) {
118         *baud = B9600;
119     } else if (cfg_baud == USERIAL_BAUD_1200) {
120         *baud = B1200;
121     } else if (cfg_baud == USERIAL_BAUD_600) {
122         *baud = B600;
123     } else {
124         HILOGE("userial vendor open: unsupported baud idx %i", cfg_baud);
125         *baud = B115200;
126         return FALSE;
127     }
128 
129     return TRUE;
130 }
131 
132 #if (BT_WAKE_VIA_USERIAL_IOCTL == TRUE)
133 /*******************************************************************************
134 **
135 ** Function        userial_ioctl_init_bt_wake
136 **
137 ** Description     helper function to set the open state of the bt_wake if ioctl
138 **                  is used. it should not hurt in the rfkill case but it might
139 **                  be better to compile it out.
140 **
141 ** Returns         none
142 **
143 *******************************************************************************/
userial_ioctl_init_bt_wake(int fd)144 void userial_ioctl_init_bt_wake(int fd)
145 {
146     uint32_t bt_wake_state;
147 
148 #if (BT_WAKE_USERIAL_LDISC == TRUE)
149     int ldisc = N_BRCM_HCI; /* brcm sleep mode support line discipline */
150 
151     /* attempt to load enable discipline driver */
152     if (ioctl(vnd_userial.fd, TIOCSETD, &ldisc) < 0) {
153         VNDUSERIALDBG(
154             "USERIAL_Open():fd %d, TIOCSETD failed: error %d for ldisc: %d", fd,
155             errno, ldisc);
156     }
157 #endif
158 
159     /* assert BT_WAKE through ioctl */
160     ioctl(fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
161     ioctl(fd, USERIAL_IOCTL_BT_WAKE_GET_ST, &bt_wake_state);
162     VNDUSERIALDBG("userial_ioctl_init_bt_wake read back BT_WAKE state=%i",
163                   bt_wake_state);
164 }
165 #endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
166 
167 /*****************************************************************************
168 **   Userial Vendor API Functions
169 *****************************************************************************/
170 
171 /*******************************************************************************
172 **
173 ** Function        userial_vendor_init
174 **
175 ** Description     Initialize userial vendor-specific control block
176 **
177 ** Returns         None
178 **
179 *******************************************************************************/
userial_vendor_init(void)180 void userial_vendor_init(void)
181 {
182     vnd_userial.fd = -1;
183     (void)snprintf_s(vnd_userial.port_name, VND_PORT_NAME_MAXLEN,
184                      VND_PORT_NAME_MAXLEN, "%s", BLUETOOTH_UART_DEVICE_PORT);
185 }
186 
187 /*******************************************************************************
188 **
189 ** Function        userial_vendor_open
190 **
191 ** Description     Open the serial port with the given configuration
192 **
193 ** Returns         device fd
194 **
195 *******************************************************************************/
userial_vendor_open(tUSERIAL_CFG * p_cfg)196 int userial_vendor_open(tUSERIAL_CFG *p_cfg)
197 {
198     uint32_t baud;
199     uint8_t data_bits;
200     uint16_t parity;
201     uint8_t stop_bits;
202 
203     (void) data_bits;
204     (void) parity;
205 
206     vnd_userial.fd = -1;
207 
208     if (!userial_to_tcio_baud(p_cfg->baud, &baud)) {
209         return -1;
210     }
211 
212     if (p_cfg->fmt & USERIAL_DATABITS_8) {
213         data_bits = CS8;
214     } else if (p_cfg->fmt & USERIAL_DATABITS_7) {
215         data_bits = CS7;
216     } else if (p_cfg->fmt & USERIAL_DATABITS_6) {
217         data_bits = CS6;
218     } else if (p_cfg->fmt & USERIAL_DATABITS_5) {
219         data_bits = CS5;
220     }
221 
222     if (p_cfg->fmt & USERIAL_PARITY_NONE) {
223         parity = 0;
224     } else if (p_cfg->fmt & USERIAL_PARITY_EVEN) {
225         parity = PARENB;
226     } else if (p_cfg->fmt & USERIAL_PARITY_ODD) {
227         parity = (PARENB | PARODD);
228     }
229 
230     if (p_cfg->fmt & USERIAL_STOPBITS_1) {
231         stop_bits = 0;
232     } else if (p_cfg->fmt & USERIAL_STOPBITS_2) {
233         stop_bits = CSTOPB;
234     }
235 
236     HILOGI("userial vendor open: opening %s", vnd_userial.port_name);
237 
238     if ((vnd_userial.fd = open(vnd_userial.port_name, O_RDWR)) == -1) {
239         HILOGI("userial vendor open: port opening error");
240         return -1;
241     }
242 
243     tcflush(vnd_userial.fd, TCIOFLUSH);
244 
245     tcgetattr(vnd_userial.fd, &vnd_userial.termios);
246     cfmakeraw(&vnd_userial.termios);
247     vnd_userial.termios.c_cflag |= (CRTSCTS | stop_bits);
248     tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
249     tcflush(vnd_userial.fd, TCIOFLUSH);
250 
251     tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
252     tcflush(vnd_userial.fd, TCIOFLUSH);
253     tcflush(vnd_userial.fd, TCIOFLUSH);
254 
255     /* set input/output baudrate */
256     cfsetospeed(&vnd_userial.termios, baud);
257     cfsetispeed(&vnd_userial.termios, baud);
258     tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
259 
260 #if (BT_WAKE_VIA_USERIAL_IOCTL == TRUE)
261     userial_ioctl_init_bt_wake(vnd_userial.fd);
262 #endif
263 
264     HILOGI("device fd = %d open", vnd_userial.fd);
265     return vnd_userial.fd;
266 }
267 
268 /*******************************************************************************
269 **
270 ** Function        userial_vendor_close
271 **
272 ** Description     Conduct vendor-specific close work
273 **
274 ** Returns         None
275 **
276 *******************************************************************************/
userial_vendor_close(void)277 void userial_vendor_close(void)
278 {
279     int result;
280 
281     if (vnd_userial.fd == -1) {
282         return;
283     }
284 
285 #if (BT_WAKE_VIA_USERIAL_IOCTL == TRUE)
286     /* de-assert bt_wake BEFORE closing port */
287     ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL);
288 #endif
289 
290     HILOGI("device fd = %d close", vnd_userial.fd);
291     // flush Tx before close to make sure no chars in buffer
292     tcflush(vnd_userial.fd, TCIOFLUSH);
293     if ((result = close(vnd_userial.fd)) < 0) {
294         HILOGE("close(fd:%d) FAILED result:%d", vnd_userial.fd, result);
295     }
296 
297     vnd_userial.fd = -1;
298 }
299 
300 /*******************************************************************************
301 **
302 ** Function        userial_vendor_set_baud
303 **
304 ** Description     Set new baud rate
305 **
306 ** Returns         None
307 **
308 *******************************************************************************/
userial_vendor_set_baud(uint8_t userial_baud)309 void userial_vendor_set_baud(uint8_t userial_baud)
310 {
311     uint32_t tcio_baud;
312 
313     if (USERIAL_VENDOR_SET_BAUD_DELAY_US > 0) {
314         usleep(USERIAL_VENDOR_SET_BAUD_DELAY_US);
315     }
316 
317     userial_to_tcio_baud(userial_baud, &tcio_baud);
318 
319     cfsetospeed(&vnd_userial.termios, tcio_baud);
320     cfsetispeed(&vnd_userial.termios, tcio_baud);
321     tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
322 }
323 
324 /*******************************************************************************
325 **
326 ** Function        userial_vendor_ioctl
327 **
328 ** Description     ioctl inteface
329 **
330 ** Returns         None
331 **
332 *******************************************************************************/
userial_vendor_ioctl(userial_vendor_ioctl_op_t op,void * p_data)333 void userial_vendor_ioctl(userial_vendor_ioctl_op_t op, void *p_data)
334 {
335     switch (op) {
336 #if (BT_WAKE_VIA_USERIAL_IOCTL == TRUE)
337         case USERIAL_OP_ASSERT_BT_WAKE:
338             VNDUSERIALDBG("## userial_vendor_ioctl: Asserting BT_Wake ##");
339             ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
340             break;
341 
342         case USERIAL_OP_DEASSERT_BT_WAKE:
343             VNDUSERIALDBG("## userial_vendor_ioctl: De-asserting BT_Wake ##");
344             ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL);
345             break;
346 
347         case USERIAL_OP_GET_BT_WAKE_STATE:
348             ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_GET_ST, p_data);
349             break;
350 #endif //  (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
351 
352         default:
353             break;
354     }
355 }
356 
357 /*******************************************************************************
358 **
359 ** Function        userial_set_port
360 **
361 ** Description     Configure UART port name
362 **
363 ** Returns         0 : Success
364 **                 Otherwise : Fail
365 **
366 *******************************************************************************/
userial_set_port(char * p_conf_name,char * p_conf_value,int param)367 int userial_set_port(char *p_conf_name, char *p_conf_value, int param)
368 {
369     (void)strcpy_s(vnd_userial.port_name, VND_PORT_NAME_MAXLEN, p_conf_value);
370 
371     return 0;
372 }
373