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