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