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