1 /******************************************************************************
2 *
3 * Copyright (C) 1999-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 #include "OverrideLog.h"
20 #include <string.h>
21 #include "gki.h"
22 #include "nfc_hal_api.h"
23 #include "nfc_hal_int.h"
24 #include "userial.h"
25 #include "nfc_target.h"
26
27 #include <pthread.h>
28 #include <termios.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <gki_int.h>
33 #include "hcidefs.h"
34 #include <poll.h>
35 #include "upio.h"
36 #include "bcm2079x.h"
37 #include "config.h"
38
39 #define HCISU_EVT EVENT_MASK(APPL_EVT_0)
40 #define MAX_ERROR 10
41 #define default_transport "/dev/bcm2079x"
42
43 #define NUM_RESET_ATTEMPTS 5
44
45 #ifndef BTE_APPL_MAX_USERIAL_DEV_NAME
46 #define BTE_APPL_MAX_USERIAL_DEV_NAME (256)
47 #endif
48 extern UINT8 appl_trace_level;
49
50 /* Mapping of USERIAL_PORT_x to linux */
51 extern UINT32 ScrProtocolTraceFlag;
52 static tUPIO_STATE current_nfc_wake_state = UPIO_OFF;
53 int uart_port = 0;
54 int isLowSpeedTransport = 0;
55 int nfc_wake_delay = 0;
56 int nfc_write_delay = 0;
57 int gPowerOnDelay = 300;
58 static int gPrePowerOffDelay = 0; // default value
59 static int gPostPowerOffDelay = 0; // default value
60 static pthread_mutex_t close_thread_mutex = PTHREAD_MUTEX_INITIALIZER;
61
62 char userial_dev[BTE_APPL_MAX_USERIAL_DEV_NAME+1];
63 char power_control_dev[BTE_APPL_MAX_USERIAL_DEV_NAME+1];
64 tSNOOZE_MODE_CONFIG gSnoozeModeCfg = {
65 NFC_HAL_LP_SNOOZE_MODE_SPI_I2C, /* Sleep Mode (0=Disabled 1=UART 8=SPI/I2C) */
66 NFC_HAL_LP_IDLE_THRESHOLD_HOST, /* Idle Threshold Host */
67 NFC_HAL_LP_IDLE_THRESHOLD_HC, /* Idle Threshold HC */
68 NFC_HAL_LP_ACTIVE_LOW, /* NFC Wake active mode (0=ActiveLow 1=ActiveHigh) */
69 NFC_HAL_LP_ACTIVE_HIGH /* Host Wake active mode (0=ActiveLow 1=ActiveHigh) */
70 };
71
72 UINT8 bcmi2cnfc_client_addr = 0;
73 UINT8 bcmi2cnfc_read_multi_packets = 0;
74
75 #define USERIAL_Debug_verbose ((ScrProtocolTraceFlag & 0x80000000) == 0x80000000)
76
77 #include <sys/socket.h>
78
79 #define LOG_TAG "USERIAL_LINUX"
80
81 static UINT8 spi_negotiation[10] = { 0xF0, /* CMD */
82 0x00, /* SPI PARM Negotiation */
83 0x01, /* SPI Version */
84 0x00, /* SPI Mode:0, SPI_INT active low */
85 0x00, /* 8Bit, MSB first, Little Endian byte order */
86 0x00, /* Reserved */
87 0xFF, /* Sleep timeout Lower Byte */
88 0xFF, /* Sleep timeout Upper Byte */
89 0x00, /* Reserved */
90 0x00 /* Reserved */
91 };
92 static UINT8 spi_nego_res[20];
93
94 #include <ctype.h>
95
96 #define USING_BRCM_USB TRUE
97
98 /* use tc interface to change baudrate instead of close/open sequence which can fail on some platforms
99 * due to tx line movement when opeing/closing the UART. the 43xx do not like this. */
100 #ifndef USERIAL_USE_TCIO_BAUD_CHANGE
101 #define USERIAL_USE_TCIO_BAUD_CHANGE FALSE
102 #endif
103
104 #ifndef USERIAL_USE_IO_BT_WAKE
105 #define USERIAL_USE_IO_BT_WAKE FALSE
106 #endif
107
108 /* this are the ioctl values used for bt_wake ioctl via UART driver. you may need to redefine at for
109 * you platform! Logically they need to be unique and not colide with existing uart ioctl's.
110 */
111 #ifndef USERIAL_IO_BT_WAKE_ASSERT
112 #define USERIAL_IO_BT_WAKE_ASSERT 0x8003
113 #endif
114 #ifndef USERIAL_IO_BT_WAKE_DEASSERT
115 #define USERIAL_IO_BT_WAKE_DEASSERT 0x8004
116 #endif
117 #ifndef USERIAL_IO_BT_WAKE_GET_ST
118 #define USERIAL_IO_BT_WAKE_GET_ST 0x8005
119 #endif
120
121 /* the read limit in this current implementation depends on the GKI_BUF3_SIZE
122 * It would be better to use some ring buffer from the USERIAL_Read() is reading
123 * instead of putting it into GKI buffers.
124 */
125 #define READ_LIMIT (USERIAL_POOL_BUF_SIZE-BT_HDR_SIZE)
126 /*
127 * minimum buffer size requirement to read a full sized packet from NFCC = 255 + 4 byte header
128 */
129 #define MIN_BUFSIZE 259
130 #define POLL_TIMEOUT 1000
131 /* priority of the reader thread */
132 #define USERIAL_READ_TRHEAD_PRIO 90
133 /* time (ms) to wait before trying to allocate again a GKI buffer */
134 #define NO_GKI_BUFFER_RECOVER_TIME 100
135 #define MAX_SERIAL_PORT (USERIAL_PORT_15 + 1)
136
137 extern void dumpbin(const char* data, int size);
138 extern UINT8 *scru_dump_hex (UINT8 *p, char *p_title, UINT32 len, UINT32 trace_layer, UINT32 trace_type);
139
140 static pthread_t worker_thread1 = 0;
141
142 typedef struct {
143 volatile unsigned long bt_wake_state;
144 int sock;
145 tUSERIAL_CBACK *ser_cb;
146 UINT16 baud;
147 UINT8 data_bits;
148 UINT16 parity;
149 UINT8 stop_bits;
150 UINT8 port;
151 tUSERIAL_OPEN_CFG open_cfg;
152 int sock_power_control;
153 int client_device_address;
154 struct timespec write_time;
155 } tLINUX_CB;
156
157 static tLINUX_CB linux_cb; /* case of multipel port support use array : [MAX_SERIAL_PORT] */
158
159 void userial_close_thread(UINT32 params);
160
161 static UINT8 device_name[20];
162 static int bSerialPortDevice = FALSE;
163 static int _timeout = POLL_TIMEOUT;
164 static BOOLEAN is_close_thread_is_waiting = FALSE;
165
166
167 int perf_log_every_count = 0;
168 typedef struct {
169 const char* label;
170 long lapse;
171 long bytes;
172 long count;
173 long overhead;
174 } tPERF_DATA;
175
176 /*******************************************************************************
177 **
178 ** Function perf_reset
179 **
180 ** Description reset performance measurement data
181 **
182 ** Returns none
183 **
184 *******************************************************************************/
perf_reset(tPERF_DATA * t)185 void perf_reset(tPERF_DATA* t)
186 {
187 t->count =
188 t->bytes =
189 t->lapse = 0;
190 }
191
192 /*******************************************************************************
193 **
194 ** Function perf_log
195 **
196 ** Description produce a log entry of cvurrent performance data
197 **
198 ** Returns none
199 **
200 *******************************************************************************/
perf_log(tPERF_DATA * t)201 void perf_log(tPERF_DATA* t)
202 {
203 // round to nearest ms
204 // t->lapse += 500;
205 // t->lapse /= 1000;
206 if (t->lapse)
207 {
208 if (t->bytes)
209 ALOGD( "%s:%s, bytes=%ld, lapse=%ld (%d.%02d kbps) (bus data rate %d.%02d kbps) overhead %d(%d percent)\n",
210 __func__,
211 t->label, t->bytes, t->lapse,
212 (int)(8 * t->bytes / t->lapse), (int)(800 * t->bytes / (t->lapse)) % 100,
213 (int)(9 * (t->bytes + t->count * t->overhead) / t->lapse), (int)(900 * (t->bytes + t->count * t->overhead) / (t->lapse)) % 100,
214 (int)(t->count * t->overhead), (int)(t->count * t->overhead * 100 / t->bytes)
215 );
216 else
217 ALOGD( "%s:%s, lapse=%ld (average %ld)\n", __func__,
218 t->label, t->lapse, (int)t->lapse / t->count
219 );
220 }
221 perf_reset(t);
222 }
223
224 /*******************************************************************************
225 **
226 ** Function perf_update
227 **
228 ** Description update perforamnce measurement data
229 **
230 ** Returns none
231 **
232 *******************************************************************************/
perf_update(tPERF_DATA * t,long lapse,long bytes)233 void perf_update(tPERF_DATA* t, long lapse, long bytes)
234 {
235 if (!perf_log_every_count)
236 return;
237 // round to nearest ms
238 lapse += 500;
239 lapse /= 1000;
240 t->count++;
241 t->bytes += bytes;
242 t->lapse += lapse;
243 if (t->count == perf_log_every_count)
244 perf_log(t);
245 }
246
247 static tPERF_DATA perf_poll = {"USERIAL_Poll", 0, 0, 0, 0};
248 static tPERF_DATA perf_read = {"USERIAL_Read", 0, 0, 0, 9};
249 static tPERF_DATA perf_write = {"USERIAL_Write", 0, 0, 0, 3};
250 static tPERF_DATA perf_poll_2_poll = {"USERIAL_Poll_to_Poll", 0, 0, 0, 0};
251 static clock_t _poll_t0 = 0;
252
253 static UINT32 userial_baud_tbl[] =
254 {
255 300, /* USERIAL_BAUD_300 0 */
256 600, /* USERIAL_BAUD_600 1 */
257 1200, /* USERIAL_BAUD_1200 2 */
258 2400, /* USERIAL_BAUD_2400 3 */
259 9600, /* USERIAL_BAUD_9600 4 */
260 19200, /* USERIAL_BAUD_19200 5 */
261 57600, /* USERIAL_BAUD_57600 6 */
262 115200, /* USERIAL_BAUD_115200 7 */
263 230400, /* USERIAL_BAUD_230400 8 */
264 460800, /* USERIAL_BAUD_460800 9 */
265 921600, /* USERIAL_BAUD_921600 10 */
266 1000000, /* USERIAL_BAUD_1M 11 */
267 1500000, /* USERIAL_BAUD_1_5M 12 */
268 2000000, /* USERIAL_BAUD_2M 13 */
269 3000000, /* USERIAL_BAUD_3M 14 */
270 4000000 /* USERIAL_BAUD_4M 15 */
271 };
272
273 /*******************************************************************************
274 **
275 ** Function wake_state
276 **
277 ** Description return current state of NFC_WAKE gpio
278 **
279 ** Returns GPIO value to wake NFCC
280 **
281 *******************************************************************************/
wake_state()282 static inline int wake_state()
283 {
284 return ((gSnoozeModeCfg.nfc_wake_active_mode == NFC_HAL_LP_ACTIVE_HIGH) ? UPIO_ON : UPIO_OFF);
285 }
286
287 /*******************************************************************************
288 **
289 ** Function sleep_state
290 **
291 ** Description return current state of NFC_WAKE gpio
292 **
293 ** Returns GPIO value to allow NFCC to goto sleep
294 **
295 *******************************************************************************/
sleep_state()296 static inline int sleep_state()
297 {
298 return ((gSnoozeModeCfg.nfc_wake_active_mode == NFC_HAL_LP_ACTIVE_HIGH) ? UPIO_OFF : UPIO_ON);
299 }
300
301 /*******************************************************************************
302 **
303 ** Function isWake
304 **
305 ** Description return current state of NFC_WAKE gpio based on the active mode setting
306 **
307 ** Returns asserted_state if it's awake, deasserted_state if it's allowed to sleep
308 **
309 *******************************************************************************/
isWake(int state)310 static inline int isWake(int state)
311 {
312 int asserted_state = ((gSnoozeModeCfg.nfc_wake_active_mode == NFC_HAL_LP_ACTIVE_HIGH) ? UPIO_ON : UPIO_OFF);
313 return (state != -1) ?
314 state == asserted_state :
315 current_nfc_wake_state == asserted_state;
316 }
317
318 /*******************************************************************************
319 **
320 ** Function setWriteDelay
321 **
322 ** Description Record a delay for the next write operation
323 **
324 ** Input Parameter delay in milliseconds
325 **
326 ** Comments use this function to register a delay before next write,
327 ** This is used in three instances: power up delay, wake delay
328 ** and write delay
329 **
330 *******************************************************************************/
setWriteDelay(int delay)331 static void setWriteDelay(int delay)
332 {
333 if (delay <= 0) {
334 // Set a minimum delay of 5ms between back-to-back writes
335 delay = 5;
336 }
337
338 clock_gettime(CLOCK_MONOTONIC, &linux_cb.write_time);
339 if (delay > 1000)
340 {
341 linux_cb.write_time.tv_sec += delay / 1000;
342 delay %= 1000;
343 }
344 unsigned long write_delay = delay * 1000 * 1000;
345 linux_cb.write_time.tv_nsec += write_delay;
346 if (linux_cb.write_time.tv_nsec > 1000*1000*1000)
347 {
348 linux_cb.write_time.tv_nsec -= 1000*1000*1000;
349 linux_cb.write_time.tv_sec++;
350 }
351 }
352
353 /*******************************************************************************
354 **
355 ** Function doWriteDelay
356 **
357 ** Description Execute a delay as registered in setWriteDelay()
358 **
359 ** Output Parameter none
360 **
361 ** Returns none
362 **
363 ** Comments This function calls GKI_Delay to execute a delay to fulfill
364 ** the delay registered earlier.
365 **
366 *******************************************************************************/
doWriteDelay()367 static void doWriteDelay()
368 {
369 struct timespec now;
370 clock_gettime(CLOCK_MONOTONIC, &now);
371 long delay = 0;
372
373 if (now.tv_sec > linux_cb.write_time.tv_sec)
374 return;
375 else if (now.tv_sec == linux_cb.write_time.tv_sec)
376 {
377 if (now.tv_nsec > linux_cb.write_time.tv_nsec)
378 return;
379 delay = (linux_cb.write_time.tv_nsec - now.tv_nsec) / 1000000;
380 }
381 else
382 delay = (linux_cb.write_time.tv_sec - now.tv_sec) * 1000 + linux_cb.write_time.tv_nsec / 1000000 - now.tv_nsec / 1000000;
383
384 if (delay > 0 && delay < 1000)
385 {
386 ALOGD_IF((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "doWriteDelay() delay %ld ms", delay);
387 GKI_delay(delay);
388 }
389 }
390
391 /*******************************************************************************
392 **
393 ** Function create_signal_fds
394 **
395 ** Description create a socketpair for read thread to use
396 **
397 ** Returns file descriptor
398 **
399 *******************************************************************************/
400
401 static int signal_fds[2];
create_signal_fds(struct pollfd * set)402 static inline int create_signal_fds(struct pollfd* set)
403 {
404 if (signal_fds[0] == 0 && socketpair(AF_UNIX, SOCK_STREAM, 0, signal_fds) < 0)
405 {
406 ALOGE("%s create_signal_sockets:socketpair failed, errno: %d", __func__, errno);
407 return -1;
408 }
409 set->fd = signal_fds[0];
410 return signal_fds[0];
411 }
412
413 /*******************************************************************************
414 **
415 ** Function close_signal_fds
416 **
417 ** Description close the socketpair
418 **
419 ** Returns none
420 **
421 *******************************************************************************/
close_signal_fds()422 static inline void close_signal_fds()
423 {
424 close(signal_fds[0]);
425 signal_fds[0] = 0;
426
427 close(signal_fds[1]);
428 signal_fds[1] = 0;
429 }
430
431 /*******************************************************************************
432 **
433 ** Function send_wakeup_signal
434 **
435 ** Description send a one byte data to the socket as signal to the read thread
436 ** for it to stop
437 **
438 ** Returns number of bytes sent, or error no
439 **
440 *******************************************************************************/
send_wakeup_signal()441 static inline int send_wakeup_signal()
442 {
443 char sig_on = 1;
444 ALOGD("%s: Sending signal to %d", __func__, signal_fds[1]);
445 return send(signal_fds[1], &sig_on, sizeof(sig_on), 0);
446 }
447
448 /*******************************************************************************
449 **
450 ** Function reset_signal
451 **
452 ** Description read the one byte data from the socket
453 **
454 ** Returns received data
455 **
456 *******************************************************************************/
reset_signal()457 static inline int reset_signal()
458 {
459 char sig_recv = 0;
460 ALOGD("%s: Receiving signal from %d", __func__, signal_fds[0]);
461 recv(signal_fds[0], &sig_recv, sizeof(sig_recv), MSG_WAITALL);
462 return (int)sig_recv;
463 }
464
465 /*******************************************************************************
466 **
467 ** Function is_signaled
468 **
469 ** Description test if there's data waiting on the socket
470 **
471 ** Returns TRUE is data is available
472 **
473 *******************************************************************************/
is_signaled(struct pollfd * set)474 static inline int is_signaled(struct pollfd* set)
475 {
476 return ((set->revents & POLLIN) == POLLIN) || ((set->revents & POLLRDNORM) == POLLRDNORM) ;
477 }
478
479 /******************************************************************************/
480
481 typedef unsigned char uchar;
482
483 BUFFER_Q Userial_in_q;
484
485 /*******************************************************************************
486 **
487 ** Function USERIAL_GetLineSpeed
488 **
489 ** Description This function convert USERIAL baud to line speed.
490 **
491 ** Output Parameter None
492 **
493 ** Returns line speed
494 **
495 *******************************************************************************/
USERIAL_GetLineSpeed(UINT8 baud)496 UDRV_API extern UINT32 USERIAL_GetLineSpeed(UINT8 baud)
497 {
498 return (baud <= USERIAL_BAUD_4M) ?
499 userial_baud_tbl[baud-USERIAL_BAUD_300] : 0;
500 }
501
502 /*******************************************************************************
503 **
504 ** Function USERIAL_GetBaud
505 **
506 ** Description This function convert line speed to USERIAL baud.
507 **
508 ** Output Parameter None
509 **
510 ** Returns line speed
511 **
512 *******************************************************************************/
USERIAL_GetBaud(UINT32 line_speed)513 UDRV_API extern UINT8 USERIAL_GetBaud(UINT32 line_speed)
514 {
515 UINT8 i;
516 for (i = USERIAL_BAUD_300; i <= USERIAL_BAUD_921600; i++)
517 {
518 if (userial_baud_tbl[i-USERIAL_BAUD_300] == line_speed)
519 return i;
520 }
521
522 return USERIAL_BAUD_AUTO;
523 }
524
525 /*******************************************************************************
526 **
527 ** Function USERIAL_Init
528 **
529 ** Description This function initializes the serial driver.
530 **
531 ** Output Parameter None
532 **
533 ** Returns Nothing
534 **
535 *******************************************************************************/
536
USERIAL_Init(void * p_cfg)537 UDRV_API void USERIAL_Init(void * p_cfg)
538 {
539 ALOGI(__FUNCTION__);
540
541 memset(&linux_cb, 0, sizeof(linux_cb));
542 linux_cb.sock = -1;
543 linux_cb.ser_cb = NULL;
544 linux_cb.sock_power_control = -1;
545 linux_cb.client_device_address = 0;
546 GKI_init_q(&Userial_in_q);
547 return;
548 }
549
550 /*******************************************************************************
551 **
552 ** Function my_read
553 **
554 ** Description This function read a packet from driver.
555 **
556 ** Output Parameter None
557 **
558 ** Returns number of bytes in the packet or error code
559 **
560 *******************************************************************************/
my_read(int fd,uchar * pbuf,int len)561 int my_read(int fd, uchar *pbuf, int len)
562 {
563 struct pollfd fds[2];
564
565 int n = 0;
566 int ret = 0;
567 int count = 0;
568 int offset = 0;
569 clock_t t1, t2;
570
571 if (!isLowSpeedTransport && _timeout != POLL_TIMEOUT)
572 ALOGD_IF((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "%s: enter, pbuf=%lx, len = %d\n", __func__, (unsigned long)pbuf, len);
573 memset(pbuf, 0, len);
574 /* need to use select in order to avoid collistion between read and close on same fd */
575 /* Initialize the input set */
576 fds[0].fd = fd;
577 fds[0].events = POLLIN | POLLERR | POLLRDNORM;
578 fds[0].revents = 0;
579
580 create_signal_fds(&fds[1]);
581 fds[1].events = POLLIN | POLLERR | POLLRDNORM;
582 fds[1].revents = 0;
583 t1 = clock();
584 n = poll(fds, 2, _timeout);
585 t2 = clock();
586 perf_update(&perf_poll, t2 - t1, 0);
587 if (_poll_t0)
588 perf_update(&perf_poll_2_poll, t2 - _poll_t0, 0);
589
590 _poll_t0 = t2;
591 /* See if there was an error */
592 if (n < 0)
593 {
594 ALOGD( "select failed; errno = %d\n", errno);
595 return -errno;
596 }
597 else if (n == 0)
598 return -EAGAIN;
599
600 if (is_signaled(&fds[1]))
601 {
602 ALOGD( "%s: exit signal received\n", __func__);
603 reset_signal();
604 return -1;
605 }
606 if (!bSerialPortDevice || len < MIN_BUFSIZE)
607 count = len;
608 else
609 count = 1;
610 do {
611 t2 = clock();
612 ret = read(fd, pbuf+offset, (size_t)count);
613 if (ret > 0)
614 perf_update(&perf_read, clock()-t2, ret);
615
616 if (ret <= 0 || !bSerialPortDevice || len < MIN_BUFSIZE)
617 break;
618
619 if (isLowSpeedTransport)
620 goto done;
621
622 if (offset == 0)
623 {
624 if (pbuf[offset] == HCIT_TYPE_NFC)
625 count = 3;
626 else if (pbuf[offset] == HCIT_TYPE_EVENT)
627 count = 2;
628 else
629 {
630 ALOGD( "%s: unknown HCIT type header pbuf[%d] = %x\n", __func__, offset, pbuf[offset]);
631 break;
632 }
633 offset = 1;
634 }
635 else if (offset == 1)
636 {
637 offset += count;
638 count = pbuf[offset-1];
639 }
640 else
641 {
642 offset += ret;
643 count -= ret;
644 }
645 if (count == 0)
646 {
647 ret = offset;
648 break;
649 }
650 } while (count > 0);
651 #if VALIDATE_PACKET
652 /*
653 * vallidate the packet structure
654 */
655 if (ret > 0 && len >= MIN_BUFSIZE)
656 {
657 count = 0;
658 while (count < ret)
659 {
660 if (pbuf[count] == HCIT_TYPE_NFC)
661 {
662 if (USERIAL_Debug_verbose)
663 scru_dump_hex(pbuf+count, NULL, pbuf[count+3]+4, 0, 0);
664 count += pbuf[count+3]+4;
665 }
666 else if (pbuf[count] == HCIT_TYPE_EVENT)
667 {
668 if (USERIAL_Debug_verbose)
669 scru_dump_hex(pbuf+count, NULL, pbuf[count+2]+3, 0, 0);
670 count += pbuf[count+2]+3;
671 }
672 else
673 {
674 ALOGD( "%s: unknown HCIT type header pbuf[%d] = %x, remain %d bytes\n", __func__, count, pbuf[count], ret-count);
675 scru_dump_hex(pbuf+count, NULL, ret - count, 0, 0);
676 break;
677 }
678 } /* while*/
679 }
680 #endif
681 done:
682 if (!isLowSpeedTransport)
683 ALOGD_IF((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "%s: return %d(0x%x) bytes, errno=%d count=%d, n=%d, timeout=%d\n", __func__,
684 ret, ret, errno, count, n, _timeout);
685 if (_timeout == POLL_TIMEOUT)
686 _timeout = -1;
687 return ret;
688 }
689 extern BOOLEAN gki_chk_buf_damage(void *p_buf);
690 static int sRxLength = 0;
691
692 /*******************************************************************************
693 **
694 ** Function userial_read_thread
695 **
696 ** Description entry point of read thread.
697 **
698 ** Output Parameter None
699 **
700 ** Returns 0
701 **
702 *******************************************************************************/
userial_read_thread(UINT32 arg)703 UINT32 userial_read_thread(UINT32 arg)
704 {
705 int rx_length;
706 int error_count = 0;
707 int bErrorReported = 0;
708 int iMaxError = MAX_ERROR;
709 BT_HDR *p_buf = NULL;
710
711 worker_thread1 = pthread_self();
712
713 ALOGD( "start userial_read_thread, id=%lx", worker_thread1);
714 _timeout = POLL_TIMEOUT;
715
716 for (;linux_cb.sock > 0;)
717 {
718 BT_HDR *p_buf;
719 UINT8 *current_packet;
720
721 if ((p_buf = (BT_HDR *) GKI_getpoolbuf( USERIAL_POOL_ID ) )!= NULL)
722 {
723 p_buf->offset = 0;
724 p_buf->layer_specific = 0;
725
726 current_packet = (UINT8 *) (p_buf + 1);
727 rx_length = my_read(linux_cb.sock, current_packet, READ_LIMIT);
728
729 }
730 else
731 {
732 ALOGE( "userial_read_thread(): unable to get buffer from GKI p_buf = %p poolid = %d\n", p_buf, USERIAL_POOL_ID);
733 rx_length = 0; /* paranoia setting */
734 GKI_delay( NO_GKI_BUFFER_RECOVER_TIME );
735 continue;
736 }
737 if (rx_length > 0)
738 {
739 bErrorReported = 0;
740 error_count = 0;
741 iMaxError = 3;
742 if (rx_length > sRxLength)
743 sRxLength = rx_length;
744 p_buf->len = (UINT16)rx_length;
745 GKI_enqueue(&Userial_in_q, p_buf);
746 if (!isLowSpeedTransport)
747 ALOGD_IF((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "userial_read_thread(): enqueued p_buf=%p, count=%d, length=%d\n",
748 p_buf, Userial_in_q.count, rx_length);
749
750 if (linux_cb.ser_cb != NULL)
751 (*linux_cb.ser_cb)(linux_cb.port, USERIAL_RX_READY_EVT, (tUSERIAL_EVT_DATA *)p_buf);
752
753 GKI_send_event(USERIAL_HAL_TASK, HCISU_EVT);
754 }
755 else
756 {
757 GKI_freebuf( p_buf );
758 if (rx_length == -EAGAIN)
759 continue;
760 else if (rx_length == -1)
761 {
762 ALOGD( "userial_read_thread(): exiting\n");
763 break;
764 }
765 else if (rx_length == 0 && !isWake(-1))
766 continue;
767 ++error_count;
768 if (rx_length <= 0 && ((error_count > 0) && ((error_count % iMaxError) == 0)))
769 {
770 if (bErrorReported == 0)
771 {
772 ALOGE( "userial_read_thread(): my_read returned (%d) error count = %d, errno=%d return USERIAL_ERR_EVT\n",
773 rx_length, error_count, errno);
774 if (linux_cb.ser_cb != NULL)
775 (*linux_cb.ser_cb)(linux_cb.port, USERIAL_ERR_EVT, (tUSERIAL_EVT_DATA *)p_buf);
776
777 GKI_send_event(USERIAL_HAL_TASK, HCISU_EVT);
778 ++bErrorReported;
779 }
780 if (sRxLength == 0)
781 {
782 ALOGE( "userial_read_thread(): my_read returned (%d) error count = %d, errno=%d exit read thread\n",
783 rx_length, error_count, errno);
784 break;
785 }
786 }
787 }
788 } /* for */
789
790 ALOGD( "userial_read_thread(): freeing GKI_buffers\n");
791 while ((p_buf = (BT_HDR *) GKI_dequeue (&Userial_in_q)) != NULL)
792 {
793 GKI_freebuf(p_buf);
794 ALOGD("userial_read_thread: dequeued buffer from Userial_in_q\n");
795 }
796
797 GKI_exit_task (GKI_get_taskid ());
798 ALOGD( "USERIAL READ: EXITING TASK\n");
799
800 return 0;
801 }
802
803 /*******************************************************************************
804 **
805 ** Function userial_to_tcio_baud
806 **
807 ** Description helper function converts USERIAL baud rates into TCIO conforming baud rates
808 **
809 ** Output Parameter None
810 **
811 ** Returns TRUE - success
812 ** FALSE - unsupported baud rate, default of 115200 is used
813 **
814 *******************************************************************************/
userial_to_tcio_baud(UINT8 cfg_baud,UINT32 * baud)815 BOOLEAN userial_to_tcio_baud(UINT8 cfg_baud, UINT32 * baud)
816 {
817 if (cfg_baud == USERIAL_BAUD_600)
818 *baud = B600;
819 else if (cfg_baud == USERIAL_BAUD_1200)
820 *baud = B1200;
821 else if (cfg_baud == USERIAL_BAUD_9600)
822 *baud = B9600;
823 else if (cfg_baud == USERIAL_BAUD_19200)
824 *baud = B19200;
825 else if (cfg_baud == USERIAL_BAUD_57600)
826 *baud = B57600;
827 else if (cfg_baud == USERIAL_BAUD_115200)
828 *baud = B115200 | CBAUDEX;
829 else if (cfg_baud == USERIAL_BAUD_230400)
830 *baud = B230400;
831 else if (cfg_baud == USERIAL_BAUD_460800)
832 *baud = B460800;
833 else if (cfg_baud == USERIAL_BAUD_921600)
834 *baud = B921600;
835 else if (cfg_baud == USERIAL_BAUD_1M)
836 *baud = B1000000;
837 else if (cfg_baud == USERIAL_BAUD_2M)
838 *baud = B2000000;
839 else if (cfg_baud == USERIAL_BAUD_3M)
840 *baud = B3000000;
841 else if (cfg_baud == USERIAL_BAUD_4M)
842 *baud = B4000000;
843 else
844 {
845 ALOGE( "USERIAL_Open: unsupported baud idx %i", cfg_baud );
846 *baud = B115200;
847 return FALSE;
848 }
849 return TRUE;
850 }
851
852 #if (USERIAL_USE_IO_BT_WAKE==TRUE)
853 /*******************************************************************************
854 **
855 ** Function userial_io_init_bt_wake
856 **
857 ** Description helper function to set the open state of the bt_wake if ioctl
858 ** is used. it should not hurt in the rfkill case but it might
859 ** be better to compile it out.
860 **
861 ** Returns none
862 **
863 *******************************************************************************/
userial_io_init_bt_wake(int fd,unsigned long * p_wake_state)864 void userial_io_init_bt_wake( int fd, unsigned long * p_wake_state )
865 {
866 /* assert BT_WAKE for ioctl. should NOT hurt on rfkill version */
867 ioctl( fd, USERIAL_IO_BT_WAKE_ASSERT, NULL);
868 ioctl( fd, USERIAL_IO_BT_WAKE_GET_ST, p_wake_state );
869 if ( *p_wake_state == 0)
870 ALOGI("\n***userial_io_init_bt_wake(): Ooops, asserted BT_WAKE signal, but still got BT_WAKE state == to %d\n",
871 *p_wake_state );
872
873 *p_wake_state = 1;
874 }
875 #endif
876
877 /*******************************************************************************
878 **
879 ** Function USERIAL_Open
880 **
881 ** Description Open the indicated serial port with the given configuration
882 **
883 ** Output Parameter None
884 **
885 ** Returns Nothing
886 **
887 *******************************************************************************/
USERIAL_Open(tUSERIAL_PORT port,tUSERIAL_OPEN_CFG * p_cfg,tUSERIAL_CBACK * p_cback)888 UDRV_API void USERIAL_Open(tUSERIAL_PORT port, tUSERIAL_OPEN_CFG *p_cfg, tUSERIAL_CBACK *p_cback)
889 {
890 UINT32 baud = 0;
891 UINT8 data_bits = 0;
892 UINT16 parity = 0;
893 UINT8 stop_bits = 0;
894 struct termios termios;
895 const char ttyusb[] = "/dev/ttyUSB";
896 const char devtty[] = "/dev/tty";
897 unsigned long num = 0;
898 int ret = 0;
899
900 ALOGI("USERIAL_Open(): enter");
901
902 //if userial_close_thread() is waiting to run; let it go first;
903 //let it finish; then continue this function
904 while (TRUE)
905 {
906 pthread_mutex_lock(&close_thread_mutex);
907 if (is_close_thread_is_waiting)
908 {
909 pthread_mutex_unlock(&close_thread_mutex);
910 ALOGI("USERIAL_Open(): wait for close-thread");
911 sleep (1);
912 }
913 else
914 break;
915 }
916
917 // restore default power off delay settings incase they were changed in userial_set_poweroff_delays()
918 gPrePowerOffDelay = 0;
919 gPostPowerOffDelay = 0;
920
921 if ( !GetStrValue ( NAME_TRANSPORT_DRIVER, userial_dev, sizeof ( userial_dev ) ) )
922 strcpy ( userial_dev, default_transport );
923 if ( GetNumValue ( NAME_UART_PORT, &num, sizeof ( num ) ) )
924 uart_port = num;
925 if ( GetNumValue ( NAME_LOW_SPEED_TRANSPORT, &num, sizeof ( num ) ) )
926 isLowSpeedTransport = num;
927 if ( GetNumValue ( NAME_NFC_WAKE_DELAY, &num, sizeof ( num ) ) )
928 nfc_wake_delay = num;
929 if ( GetNumValue ( NAME_NFC_WRITE_DELAY, &num, sizeof ( num ) ) )
930 nfc_write_delay = num;
931 if ( GetNumValue ( NAME_PERF_MEASURE_FREQ, &num, sizeof ( num ) ) )
932 perf_log_every_count = num;
933 if ( GetNumValue ( NAME_POWER_ON_DELAY, &num, sizeof ( num ) ) )
934 gPowerOnDelay = num;
935 if ( GetNumValue ( NAME_PRE_POWER_OFF_DELAY, &num, sizeof ( num ) ) )
936 gPrePowerOffDelay = num;
937 if ( GetNumValue ( NAME_POST_POWER_OFF_DELAY, &num, sizeof ( num ) ) )
938 gPostPowerOffDelay = num;
939 ALOGI("USERIAL_Open() device: %s port=%d, uart_port=%d WAKE_DELAY(%d) WRITE_DELAY(%d) POWER_ON_DELAY(%d) PRE_POWER_OFF_DELAY(%d) POST_POWER_OFF_DELAY(%d)",
940 (char*)userial_dev, port, uart_port, nfc_wake_delay, nfc_write_delay, gPowerOnDelay, gPrePowerOffDelay,
941 gPostPowerOffDelay);
942 GetStrValue( NAME_SNOOZE_MODE_CFG, (char*)&gSnoozeModeCfg, sizeof(gSnoozeModeCfg) );
943
944 strcpy((char*)device_name, (char*)userial_dev);
945 sRxLength = 0;
946 _poll_t0 = 0;
947
948 if ((strncmp(userial_dev, ttyusb, sizeof(ttyusb)-1) == 0) ||
949 (strncmp(userial_dev, devtty, sizeof(devtty)-1) == 0) )
950 {
951 if (uart_port >= MAX_SERIAL_PORT)
952 {
953 ALOGD( "Port > MAX_SERIAL_PORT\n");
954 goto done_open;
955 }
956 bSerialPortDevice = TRUE;
957 sprintf((char*)device_name, "%s%d", (char*)userial_dev, uart_port);
958 ALOGI("USERIAL_Open() using device_name: %s ", (char*)device_name);
959 if (!userial_to_tcio_baud(p_cfg->baud, &baud))
960 goto done_open;
961
962 if (p_cfg->fmt & USERIAL_DATABITS_8)
963 data_bits = CS8;
964 else if (p_cfg->fmt & USERIAL_DATABITS_7)
965 data_bits = CS7;
966 else if (p_cfg->fmt & USERIAL_DATABITS_6)
967 data_bits = CS6;
968 else if (p_cfg->fmt & USERIAL_DATABITS_5)
969 data_bits = CS5;
970 else
971 goto done_open;
972
973 if (p_cfg->fmt & USERIAL_PARITY_NONE)
974 parity = 0;
975 else if (p_cfg->fmt & USERIAL_PARITY_EVEN)
976 parity = PARENB;
977 else if (p_cfg->fmt & USERIAL_PARITY_ODD)
978 parity = (PARENB | PARODD);
979 else
980 goto done_open;
981
982 if (p_cfg->fmt & USERIAL_STOPBITS_1)
983 stop_bits = 0;
984 else if (p_cfg->fmt & USERIAL_STOPBITS_2)
985 stop_bits = CSTOPB;
986 else
987 goto done_open;
988 }
989 else
990 strcpy((char*)device_name, (char*)userial_dev);
991
992 {
993 ALOGD("%s Opening %s\n", __FUNCTION__, device_name);
994 if ((linux_cb.sock = open((char*)device_name, O_RDWR | O_NOCTTY )) == -1)
995 {
996 ALOGI("%s unable to open %s", __FUNCTION__, device_name);
997 GKI_send_event(NFC_HAL_TASK, NFC_HAL_TASK_EVT_TERMINATE);
998 goto done_open;
999 }
1000 ALOGD( "sock = %d\n", linux_cb.sock);
1001 if (GetStrValue ( NAME_POWER_CONTROL_DRIVER, power_control_dev, sizeof ( power_control_dev ) ) &&
1002 power_control_dev[0] != '\0')
1003 {
1004 if (strcmp(power_control_dev, userial_dev) == 0)
1005 linux_cb.sock_power_control = linux_cb.sock;
1006 else
1007 {
1008 if ((linux_cb.sock_power_control = open((char*)power_control_dev, O_RDWR | O_NOCTTY )) == -1)
1009 {
1010 ALOGI("%s unable to open %s", __FUNCTION__, power_control_dev);
1011 }
1012 }
1013 }
1014
1015 USERIAL_PowerupDevice(port);
1016 }
1017
1018 linux_cb.ser_cb = p_cback;
1019 linux_cb.port = port;
1020 memcpy(&linux_cb.open_cfg, p_cfg, sizeof(tUSERIAL_OPEN_CFG));
1021 GKI_create_task ((TASKPTR)userial_read_thread, USERIAL_HAL_TASK, (INT8*)"USERIAL_HAL_TASK", 0, 0, (pthread_cond_t*)NULL, NULL);
1022
1023
1024 #if (defined USERIAL_DEBUG) && (USERIAL_DEBUG == TRUE)
1025 ALOGD( "Leaving USERIAL_Open\n");
1026 #endif
1027
1028 #if (SERIAL_AMBA == TRUE)
1029 /* give 20ms time for reader thread */
1030 GKI_delay(20);
1031 #endif
1032
1033 done_open:
1034 pthread_mutex_unlock(&close_thread_mutex);
1035 ALOGI("USERIAL_Open(): exit");
1036 return;
1037 }
1038
1039 /*******************************************************************************
1040 **
1041 ** Function USERIAL_Read
1042 **
1043 ** Description Read data from a serial port using byte buffers.
1044 **
1045 ** Output Parameter None
1046 **
1047 ** Returns Number of bytes actually read from the serial port and
1048 ** copied into p_data. This may be less than len.
1049 **
1050 *******************************************************************************/
1051
1052 static BT_HDR *pbuf_USERIAL_Read = NULL;
1053
USERIAL_Read(tUSERIAL_PORT port,UINT8 * p_data,UINT16 len)1054 UDRV_API UINT16 USERIAL_Read(tUSERIAL_PORT port, UINT8 *p_data, UINT16 len)
1055 {
1056 UINT16 total_len = 0;
1057 UINT16 copy_len = 0;
1058 UINT8 * current_packet = NULL;
1059
1060 #if (defined USERIAL_DEBUG) && (USERIAL_DEBUG == TRUE)
1061 ALOGD( "%s ++ len=%d pbuf_USERIAL_Read=%p, p_data=%p\n", __func__, len, pbuf_USERIAL_Read, p_data);
1062 #endif
1063 do
1064 {
1065 if (pbuf_USERIAL_Read != NULL)
1066 {
1067 current_packet = ((UINT8 *)(pbuf_USERIAL_Read + 1)) + (pbuf_USERIAL_Read->offset);
1068
1069 if ((pbuf_USERIAL_Read->len) <= (len - total_len))
1070 copy_len = pbuf_USERIAL_Read->len;
1071 else
1072 copy_len = (len - total_len);
1073
1074 memcpy((p_data + total_len), current_packet, copy_len);
1075
1076 total_len += copy_len;
1077
1078 pbuf_USERIAL_Read->offset += copy_len;
1079 pbuf_USERIAL_Read->len -= copy_len;
1080
1081 if (pbuf_USERIAL_Read->len == 0)
1082 {
1083 GKI_freebuf(pbuf_USERIAL_Read);
1084 pbuf_USERIAL_Read = NULL;
1085 }
1086 }
1087
1088 if (pbuf_USERIAL_Read == NULL && (total_len < len))
1089 pbuf_USERIAL_Read = (BT_HDR *)GKI_dequeue(&Userial_in_q);
1090
1091 } while ((pbuf_USERIAL_Read != NULL) && (total_len < len));
1092
1093 #if (defined USERIAL_DEBUG) && (USERIAL_DEBUG == TRUE)
1094 ALOGD( "%s: returned %d bytes", __func__, total_len);
1095 #endif
1096 return total_len;
1097 }
1098
1099 /*******************************************************************************
1100 **
1101 ** Function USERIAL_Readbuf
1102 **
1103 ** Description Read data from a serial port using GKI buffers.
1104 **
1105 ** Output Parameter Pointer to a GKI buffer which contains the data.
1106 **
1107 ** Returns Nothing
1108 **
1109 ** Comments The caller of this function is responsible for freeing the
1110 ** GKI buffer when it is finished with the data. If there is
1111 ** no data to be read, the value of the returned pointer is
1112 ** NULL.
1113 **
1114 *******************************************************************************/
1115
USERIAL_ReadBuf(tUSERIAL_PORT port,BT_HDR ** p_buf)1116 UDRV_API void USERIAL_ReadBuf(tUSERIAL_PORT port, BT_HDR **p_buf)
1117 {
1118
1119 }
1120
1121 /*******************************************************************************
1122 **
1123 ** Function USERIAL_WriteBuf
1124 **
1125 ** Description Write data to a serial port using a GKI buffer.
1126 **
1127 ** Output Parameter None
1128 **
1129 ** Returns TRUE if buffer accepted for write.
1130 ** FALSE if there is already a buffer being processed.
1131 **
1132 ** Comments The buffer will be freed by the serial driver. Therefore,
1133 ** the application calling this function must not free the
1134 ** buffer.
1135 **
1136 *******************************************************************************/
1137
USERIAL_WriteBuf(tUSERIAL_PORT port,BT_HDR * p_buf)1138 UDRV_API BOOLEAN USERIAL_WriteBuf(tUSERIAL_PORT port, BT_HDR *p_buf)
1139 {
1140 return FALSE;
1141 }
1142
1143 /*******************************************************************************
1144 **
1145 ** Function USERIAL_Write
1146 **
1147 ** Description Write data to a serial port using a byte buffer.
1148 **
1149 ** Output Parameter None
1150 **
1151 ** Returns Number of bytes actually written to the transport. This
1152 ** may be less than len.
1153 **
1154 *******************************************************************************/
USERIAL_Write(tUSERIAL_PORT port,UINT8 * p_data,UINT16 len)1155 UDRV_API UINT16 USERIAL_Write(tUSERIAL_PORT port, UINT8 *p_data, UINT16 len)
1156 {
1157 int ret = 0, total = 0;
1158 int i = 0;
1159 clock_t t;
1160
1161 doWriteDelay();
1162 ALOGD_IF((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "USERIAL_Write: (%d bytes) - \n", len);
1163 t = clock();
1164 while (len != 0)
1165 {
1166 ret = write(linux_cb.sock, p_data + total, len);
1167 if (ret < 0)
1168 break;
1169 total += ret;
1170 len -= ret;
1171 }
1172 perf_update(&perf_write, clock() - t, total);
1173
1174 ALOGD_IF((appl_trace_level>=BT_TRACE_LEVEL_DEBUG), "USERIAL_Write len = %d, ret = %d, errno = %d\n", len, ret, errno);
1175
1176 /* register a delay for next write
1177 */
1178 setWriteDelay(total * nfc_write_delay / 1000);
1179 return ((UINT16)total);
1180 }
1181
1182 /*******************************************************************************
1183 **
1184 ** Function userial_change_rate
1185 **
1186 ** Description change naud rate
1187 **
1188 ** Output Parameter None
1189 **
1190 ** Returns None
1191 **
1192 *******************************************************************************/
userial_change_rate(UINT8 baud)1193 void userial_change_rate(UINT8 baud)
1194 {
1195 #if defined (USING_BRCM_USB) && (USING_BRCM_USB == FALSE)
1196 struct termios termios;
1197 #endif
1198 #if (USERIAL_USE_TCIO_BAUD_CHANGE==TRUE)
1199 UINT32 tcio_baud;
1200 #endif
1201
1202 #if defined (USING_BRCM_USB) && (USING_BRCM_USB == FALSE)
1203 tcflush(linux_cb.sock, TCIOFLUSH);
1204
1205 tcgetattr(linux_cb.sock, &termios);
1206
1207 cfmakeraw(&termios);
1208 cfsetospeed(&termios, baud);
1209 cfsetispeed(&termios, baud);
1210
1211 termios.c_cflag |= (CLOCAL | CREAD | CRTSCTS | stop_bits);
1212
1213 tcsetattr(linux_cb.sock, TCSANOW, &termios);
1214 tcflush(linux_cb.sock, TCIOFLUSH);
1215
1216 #else
1217 #if (USERIAL_USE_TCIO_BAUD_CHANGE==FALSE)
1218 fprintf(stderr, "userial_change_rate: Closing UART Port\n");
1219 ALOGI("userial_change_rate: Closing UART Port\n");
1220 USERIAL_Close(linux_cb.port);
1221
1222 GKI_delay(50);
1223
1224 /* change baud rate in settings - leave everything else the same */
1225 linux_cb.open_cfg.baud = baud;
1226
1227 ALOGD( "userial_change_rate: Attempting to reopen the UART Port at 0x%08x\n", (unsigned int)USERIAL_GetLineSpeed(baud));
1228 ALOGI("userial_change_rate: Attempting to reopen the UART Port at %i\n", (unsigned int)USERIAL_GetLineSpeed(baud));
1229
1230 USERIAL_Open(linux_cb.port, &linux_cb.open_cfg, linux_cb.ser_cb);
1231 #else /* amba uart */
1232 fprintf(stderr, "userial_change_rate(): changeing baud rate via TCIO \n");
1233 ALOGI( "userial_change_rate: (): changeing baud rate via TCIO \n");
1234 /* change baud rate in settings - leave everything else the same */
1235 linux_cb.open_cfg.baud = baud;
1236 if (!userial_to_tcio_baud(linux_cb.open_cfg.baud, &tcio_baud))
1237 return;
1238
1239 tcflush(linux_cb.sock, TCIOFLUSH);
1240
1241 /* get current settings. they should be fine besides baud rate we want to change */
1242 tcgetattr(linux_cb.sock, &termios);
1243
1244 /* set input/output baudrate */
1245 cfsetospeed(&termios, tcio_baud);
1246 cfsetispeed(&termios, tcio_baud);
1247 tcsetattr(linux_cb.sock, TCSANOW, &termios);
1248
1249 tcflush(linux_cb.sock, TCIOFLUSH);
1250 #endif
1251 #endif /* USING_BRCM_USB */
1252 }
1253
1254 /*******************************************************************************
1255 **
1256 ** Function userial_close_port
1257 **
1258 ** Description close the transport driver
1259 **
1260 ** Returns Nothing
1261 **
1262 *******************************************************************************/
userial_close_port(void)1263 void userial_close_port( void )
1264 {
1265 USERIAL_Close(linux_cb.port);
1266 }
1267
1268 /*******************************************************************************
1269 **
1270 ** Function USERIAL_Ioctl
1271 **
1272 ** Description Perform an operation on a serial port.
1273 **
1274 ** Output Parameter The p_data parameter is either an input or output depending
1275 ** on the operation.
1276 **
1277 ** Returns Nothing
1278 **
1279 *******************************************************************************/
1280
USERIAL_Ioctl(tUSERIAL_PORT port,tUSERIAL_OP op,tUSERIAL_IOCTL_DATA * p_data)1281 UDRV_API void USERIAL_Ioctl(tUSERIAL_PORT port, tUSERIAL_OP op, tUSERIAL_IOCTL_DATA *p_data)
1282 {
1283 #if (defined LINUX_OS) && (LINUX_OS == TRUE)
1284 USB_SCO_CONTROL ioctl_data;
1285
1286 /* just ignore port parameter as we are using USB in this case */
1287 #endif
1288
1289 switch (op)
1290 {
1291 case USERIAL_OP_FLUSH:
1292 break;
1293 case USERIAL_OP_FLUSH_RX:
1294 break;
1295 case USERIAL_OP_FLUSH_TX:
1296 break;
1297 case USERIAL_OP_BAUD_WR:
1298 ALOGI( "USERIAL_Ioctl: Received USERIAL_OP_BAUD_WR on port: %d, ioctl baud%i\n", port, p_data->baud);
1299 linux_cb.port = port;
1300 userial_change_rate(p_data->baud);
1301 break;
1302
1303 default:
1304 break;
1305 }
1306
1307 return;
1308 }
1309
1310 /*******************************************************************************
1311 **
1312 ** Function USERIAL_Close
1313 **
1314 ** Description Close a serial port
1315 **
1316 ** Output Parameter None
1317 **
1318 ** Returns Nothing
1319 **
1320 *******************************************************************************/
USERIAL_Close(tUSERIAL_PORT port)1321 UDRV_API void USERIAL_Close(tUSERIAL_PORT port)
1322 {
1323 pthread_attr_t attr;
1324 pthread_t close_thread;
1325
1326 ALOGD ("%s: enter", __FUNCTION__);
1327 // check to see if thread is already running
1328 if (pthread_mutex_trylock(&close_thread_mutex) == 0)
1329 {
1330 // mutex aquired so thread is not running
1331 is_close_thread_is_waiting = TRUE;
1332 pthread_mutex_unlock(&close_thread_mutex);
1333
1334 // close transport in a new thread so we don't block the caller
1335 // make thread detached, no other thread will join
1336 pthread_attr_init(&attr);
1337 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1338 pthread_create( &close_thread, &attr, (void *)userial_close_thread,(void*)port);
1339 pthread_attr_destroy(&attr);
1340 }
1341 else
1342 {
1343 // mutex not aquired to thread is already running
1344 ALOGD( "USERIAL_Close(): already closing \n");
1345 }
1346 ALOGD ("%s: exit", __FUNCTION__);
1347 }
1348
1349
1350 /*******************************************************************************
1351 **
1352 ** Function userial_close_thread
1353 **
1354 ** Description Thread to close USERIAL
1355 **
1356 ** Returns None.
1357 **
1358 *******************************************************************************/
userial_close_thread(UINT32 params)1359 void userial_close_thread(UINT32 params)
1360 {
1361 tUSERIAL_PORT port = (tUSERIAL_PORT )params;
1362 BT_HDR *p_buf = NULL;
1363 int result;
1364
1365 ALOGD( "%s: closing transport (%d)\n", __FUNCTION__, linux_cb.sock);
1366 pthread_mutex_lock(&close_thread_mutex);
1367 is_close_thread_is_waiting = FALSE;
1368
1369 if (linux_cb.sock <= 0)
1370 {
1371 ALOGD( "%s: already closed (%d)\n", __FUNCTION__, linux_cb.sock);
1372 pthread_mutex_unlock(&close_thread_mutex);
1373 return;
1374 }
1375
1376 send_wakeup_signal();
1377 result = pthread_join( worker_thread1, NULL );
1378 if ( result < 0 )
1379 ALOGE( "%s: pthread_join() FAILED: result: %d", __FUNCTION__, result );
1380 else
1381 ALOGD( "%s: pthread_join() joined: result: %d", __FUNCTION__, result );
1382
1383 if (linux_cb.sock_power_control > 0)
1384 {
1385 result = ioctl(linux_cb.sock_power_control, BCMNFC_WAKE_CTL, sleep_state());
1386 ALOGD("%s: Delay %dms before turning off the chip", __FUNCTION__, gPrePowerOffDelay);
1387 GKI_delay(gPrePowerOffDelay);
1388 result = ioctl(linux_cb.sock_power_control, BCMNFC_POWER_CTL, 0);
1389 ALOGD("%s: Delay %dms after turning off the chip", __FUNCTION__, gPostPowerOffDelay);
1390 GKI_delay(gPostPowerOffDelay);
1391 }
1392 result = close(linux_cb.sock);
1393 if (result<0)
1394 ALOGD("%s: close return %d", __FUNCTION__, result);
1395
1396 if (linux_cb.sock_power_control > 0 && linux_cb.sock_power_control != linux_cb.sock)
1397 result = close(linux_cb.sock_power_control);
1398 if (result<0)
1399 ALOGD("%s: close return %d", __FUNCTION__, result);
1400
1401 linux_cb.sock_power_control = -1;
1402 linux_cb.sock = -1;
1403
1404 close_signal_fds();
1405 pthread_mutex_unlock(&close_thread_mutex);
1406 ALOGD("%s: exiting", __FUNCTION__);
1407 }
1408
1409 /*******************************************************************************
1410 **
1411 ** Function USERIAL_Feature
1412 **
1413 ** Description Check whether a feature of the serial API is supported.
1414 **
1415 ** Output Parameter None
1416 **
1417 ** Returns TRUE if the feature is supported
1418 ** FALSE if the feature is not supported
1419 **
1420 *******************************************************************************/
1421
USERIAL_Feature(tUSERIAL_FEATURE feature)1422 UDRV_API BOOLEAN USERIAL_Feature(tUSERIAL_FEATURE feature)
1423 {
1424 switch (feature)
1425 {
1426 case USERIAL_FEAT_PORT_1:
1427 case USERIAL_FEAT_PORT_2:
1428 case USERIAL_FEAT_PORT_3:
1429 case USERIAL_FEAT_PORT_4:
1430
1431 case USERIAL_FEAT_BAUD_600:
1432 case USERIAL_FEAT_BAUD_1200:
1433 case USERIAL_FEAT_BAUD_9600:
1434 case USERIAL_FEAT_BAUD_19200:
1435 case USERIAL_FEAT_BAUD_57600:
1436 case USERIAL_FEAT_BAUD_115200:
1437
1438 case USERIAL_FEAT_STOPBITS_1:
1439 case USERIAL_FEAT_STOPBITS_2:
1440
1441 case USERIAL_FEAT_PARITY_NONE:
1442 case USERIAL_FEAT_PARITY_EVEN:
1443 case USERIAL_FEAT_PARITY_ODD:
1444
1445 case USERIAL_FEAT_DATABITS_5:
1446 case USERIAL_FEAT_DATABITS_6:
1447 case USERIAL_FEAT_DATABITS_7:
1448 case USERIAL_FEAT_DATABITS_8:
1449
1450 case USERIAL_FEAT_FC_HW:
1451 case USERIAL_FEAT_BUF_BYTE:
1452
1453 case USERIAL_FEAT_OP_FLUSH_RX:
1454 case USERIAL_FEAT_OP_FLUSH_TX:
1455 return TRUE;
1456 default:
1457 return FALSE;
1458 }
1459
1460 return FALSE;
1461 }
1462
1463 /*****************************************************************************
1464 **
1465 ** Function UPIO_Set
1466 **
1467 ** Description
1468 ** This function sets one or more GPIO devices to the given state.
1469 ** Multiple GPIOs of the same type can be masked together to set more
1470 ** than one GPIO. This function can only be used on types UPIO_LED and
1471 ** UPIO_GENERAL.
1472 **
1473 ** Input Parameters:
1474 ** type The type of device.
1475 ** pio Indicates the particular GPIOs.
1476 ** state The desired state.
1477 **
1478 ** Output Parameter:
1479 ** None.
1480 **
1481 ** Returns:
1482 ** None.
1483 **
1484 *****************************************************************************/
UPIO_Set(tUPIO_TYPE type,tUPIO pio,tUPIO_STATE new_state)1485 UDRV_API void UPIO_Set(tUPIO_TYPE type, tUPIO pio, tUPIO_STATE new_state)
1486 {
1487 int ret;
1488 if (type == UPIO_GENERAL)
1489 {
1490 if (pio == NFC_HAL_LP_NFC_WAKE_GPIO)
1491 {
1492 if (new_state == UPIO_ON || new_state == UPIO_OFF)
1493 {
1494 if (linux_cb.sock_power_control > 0)
1495 {
1496 ALOGD("%s: ioctl, state=%d", __func__, new_state);
1497 ret = ioctl(linux_cb.sock_power_control, BCMNFC_WAKE_CTL, new_state);
1498 if (isWake(new_state) && nfc_wake_delay > 0 && new_state != current_nfc_wake_state)
1499 {
1500 ALOGD("%s: ioctl, old state=%d, insert delay for %d ms", __func__, current_nfc_wake_state, nfc_wake_delay);
1501 setWriteDelay(nfc_wake_delay);
1502 }
1503 current_nfc_wake_state = new_state;
1504 }
1505 }
1506 }
1507 }
1508 }
1509
1510 /*****************************************************************************
1511 **
1512 ** Function setReadPacketSize
1513 **
1514 ** Description
1515 ** This function sets the packetSize to the driver.
1516 ** this enables faster read operation of NCI/HCI responses
1517 **
1518 ** Input Parameters:
1519 ** len number of bytes to read per operation.
1520 **
1521 ** Output Parameter:
1522 ** None.
1523 **
1524 ** Returns:
1525 ** None.
1526 **
1527 *****************************************************************************/
setReadPacketSize(int len)1528 void setReadPacketSize(int len)
1529 {
1530 int ret;
1531 ALOGD("%s: ioctl, len=%d", __func__, len);
1532 ret = ioctl(linux_cb.sock, BCMNFC_READ_FULL_PACKET, len);
1533 }
1534
1535
USERIAL_IsClosed()1536 UDRV_API BOOLEAN USERIAL_IsClosed()
1537 {
1538 return (linux_cb.sock == -1) ? TRUE : FALSE;
1539 }
1540
USERIAL_PowerupDevice(tUSERIAL_PORT port)1541 UDRV_API void USERIAL_PowerupDevice(tUSERIAL_PORT port)
1542 {
1543 int ret = -1;
1544 unsigned long num = 0;
1545 unsigned int resetSuccess = 0;
1546 unsigned int numTries = 0;
1547 unsigned char spi_negotiation[64];
1548
1549 if ( GetNumValue ( NAME_READ_MULTI_PACKETS, &num, sizeof ( num ) ) )
1550 bcmi2cnfc_read_multi_packets = num;
1551
1552 if (bcmi2cnfc_read_multi_packets > 0)
1553 ioctl(linux_cb.sock, BCMNFC_READ_MULTI_PACKETS, bcmi2cnfc_read_multi_packets);
1554
1555 while (!resetSuccess && numTries < NUM_RESET_ATTEMPTS) {
1556 if (numTries++ > 0) {
1557 ALOGW("BCM2079x: retrying reset, attempt %d/%d", numTries, NUM_RESET_ATTEMPTS);
1558 }
1559 if (linux_cb.sock_power_control > 0)
1560 {
1561 ioctl(linux_cb.sock_power_control, BCMNFC_POWER_CTL, 0);
1562 GKI_delay(10);
1563 ioctl(linux_cb.sock_power_control, BCMNFC_POWER_CTL, 1);
1564 current_nfc_wake_state = wake_state();
1565 ret = ioctl(linux_cb.sock_power_control, BCMNFC_WAKE_CTL, current_nfc_wake_state);
1566 }
1567
1568 ret = GetStrValue ( NAME_SPI_NEGOTIATION, (char*)spi_negotiation, sizeof ( spi_negotiation ) );
1569 if (ret > 0 && spi_negotiation[0] > 0 && spi_negotiation[0] < sizeof ( spi_negotiation ) - 1)
1570 {
1571 int len = spi_negotiation[0];
1572 /* Wake control is not available: Start SPI negotiation*/
1573 USERIAL_Write(port, &spi_negotiation[1], len);
1574 USERIAL_Read(port, spi_negotiation, sizeof ( spi_negotiation ));
1575 }
1576
1577 if ( GetNumValue ( NAME_CLIENT_ADDRESS, &num, sizeof ( num ) ) )
1578 bcmi2cnfc_client_addr = num & 0xFF;
1579 if (bcmi2cnfc_client_addr != 0 &&
1580 0x07 < bcmi2cnfc_client_addr &&
1581 bcmi2cnfc_client_addr < 0x78)
1582 {
1583 ALOGD( "Change client address to %x\n", bcmi2cnfc_client_addr);
1584 GKI_delay(gPowerOnDelay);
1585 ret = ioctl(linux_cb.sock, BCMNFC_CHANGE_ADDR, bcmi2cnfc_client_addr);
1586 if (!ret) {
1587 resetSuccess = 1;
1588 linux_cb.client_device_address = bcmi2cnfc_client_addr;
1589 }
1590 } else {
1591 resetSuccess = 1;
1592 }
1593 }
1594
1595 if (!resetSuccess) {
1596 ALOGE("BCM2079x: failed to initialize NFC controller");
1597 }
1598 GKI_delay(gPowerOnDelay);
1599 }
1600