Home | History | Annotate | Download | only in pal
      1 /******************************************************************************
      2  *
      3  *  Copyright 2018 NXP
      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 #define LOG_TAG "NxpEseHal"
     19 #include <log/log.h>
     20 
     21 #include <errno.h>
     22 #include <fcntl.h>
     23 #include <stdlib.h>
     24 #include <sys/ioctl.h>
     25 #include <unistd.h>
     26 
     27 #include <ese_config.h>
     28 #include <phEseStatus.h>
     29 #include <phNxpEsePal_spi.h>
     30 #include <string.h>
     31 
     32 extern bool ese_debug_enabled;
     33 
     34 /*!
     35  * \brief Normal mode header length
     36  */
     37 #define NORMAL_MODE_HEADER_LEN 3
     38 /*!
     39  * \brief Normal mode header offset
     40  */
     41 #define NORMAL_MODE_LEN_OFFSET 2
     42 /*!
     43  * \brief Start of frame marker
     44  */
     45 #define SEND_PACKET_SOF 0x5A
     46 /*!
     47  * \brief To enable SPI interface for ESE communication
     48  */
     49 #define SPI_ENABLED 1
     50 /*******************************************************************************
     51 **
     52 ** Function         phPalEse_close
     53 **
     54 ** Description      Closes PN547 device
     55 **
     56 ** Parameters       pDevHandle - device handle
     57 **
     58 ** Returns          None
     59 **
     60 *******************************************************************************/
     61 void phPalEse_close(void* pDevHandle) {
     62   if (NULL != pDevHandle) {
     63 #ifdef SPI_ENABLED
     64     phPalEse_spi_close(pDevHandle);
     65 #else
     66 /* RFU */
     67 #endif
     68   }
     69   return;
     70 }
     71 
     72 /*******************************************************************************
     73 **
     74 ** Function         phPalEse_open_and_configure
     75 **
     76 ** Description      Open and configure ESE device
     77 **
     78 ** Parameters       pConfig     - hardware information
     79 **
     80 ** Returns          ESE status:
     81 **                  ESESTATUS_SUCCESS            - open_and_configure operation
     82 *success
     83 **                  ESESTATUS_INVALID_DEVICE     - device open operation failure
     84 **
     85 *******************************************************************************/
     86 ESESTATUS phPalEse_open_and_configure(pphPalEse_Config_t pConfig) {
     87   ESESTATUS status = ESESTATUS_FAILED;
     88 #ifdef SPI_ENABLED
     89   status = phPalEse_spi_open_and_configure(pConfig);
     90 #else
     91 /* RFU */
     92 #endif
     93   return status;
     94 }
     95 
     96 /*******************************************************************************
     97 **
     98 ** Function         phPalEse_read
     99 **
    100 ** Description      Reads requested number of bytes from pn547 device into given
    101 *buffer
    102 **
    103 ** Parameters       pDevHandle       - valid device handle
    104 **                  pBuffer          - buffer for read data
    105 **                  nNbBytesToRead   - number of bytes requested to be read
    106 **
    107 ** Returns          numRead   - number of successfully read bytes
    108 **                  -1        - read operation failure
    109 **
    110 *******************************************************************************/
    111 int phPalEse_read(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToRead) {
    112   int ret = -1;
    113 #ifdef SPI_ENABLED
    114   ret = phPalEse_spi_read(pDevHandle, pBuffer, nNbBytesToRead);
    115 #else
    116 /* RFU */
    117 #endif
    118   return ret;
    119 }
    120 
    121 /*******************************************************************************
    122 **
    123 ** Function         phPalEse_write
    124 **
    125 ** Description      Writes requested number of bytes from given buffer into
    126 *pn547 device
    127 **
    128 ** Parameters       pDevHandle       - valid device handle
    129 **                  pBuffer          - buffer for read data
    130 **                  nNbBytesToWrite  - number of bytes requested to be written
    131 **
    132 ** Returns          numWrote   - number of successfully written bytes
    133 **                  -1         - write operation failure
    134 **
    135 *******************************************************************************/
    136 int phPalEse_write(void* pDevHandle, uint8_t* pBuffer, int nNbBytesToWrite) {
    137   int numWrote = 0;
    138 
    139   if (NULL == pDevHandle) {
    140     return -1;
    141   }
    142 #ifdef SPI_ENABLED
    143   numWrote = phPalEse_spi_write(pDevHandle, pBuffer, nNbBytesToWrite);
    144 #else
    145 /* RFU */
    146 #endif
    147   return numWrote;
    148 }
    149 
    150 /*******************************************************************************
    151 **
    152 ** Function         phPalEse_ioctl
    153 **
    154 ** Description      Exposed ioctl by p61 spi driver
    155 **
    156 ** Parameters       pDevHandle     - valid device handle
    157 **                  level          - reset level
    158 **
    159 ** Returns           0   - ioctl operation success
    160 **                  -1   - ioctl operation failure
    161 **
    162 *******************************************************************************/
    163 ESESTATUS phPalEse_ioctl(phPalEse_ControlCode_t eControlCode, void* pDevHandle,
    164                          long level) {
    165   ESESTATUS ret = ESESTATUS_FAILED;
    166   ALOGD_IF(ese_debug_enabled, "phPalEse_spi_ioctl(), ioctl %x , level %lx",
    167            eControlCode, level);
    168 
    169   if (NULL == pDevHandle) {
    170     return ESESTATUS_IOCTL_FAILED;
    171   }
    172 #ifdef SPI_ENABLED
    173   ret = phPalEse_spi_ioctl(eControlCode, pDevHandle, level);
    174 #else
    175 /* RFU */
    176 #endif
    177   return ret;
    178 }
    179 
    180 /*******************************************************************************
    181 **
    182 ** Function         phPalEse_print_packet
    183 **
    184 ** Description      Print packet
    185 **
    186 ** Returns          None
    187 **
    188 *******************************************************************************/
    189 void phPalEse_print_packet(const char* pString, const uint8_t* p_data,
    190                            uint16_t len) {
    191   uint32_t i;
    192   char print_buffer[len * 3 + 1];
    193 
    194   memset(print_buffer, 0, sizeof(print_buffer));
    195   for (i = 0; i < len; i++) {
    196     snprintf(&print_buffer[i * 2], 3, "%02X", p_data[i]);
    197   }
    198   if (0 == memcmp(pString, "SEND", 0x04)) {
    199     ALOGD_IF(ese_debug_enabled, "NxpEseDataX len = %3d > %s", len,
    200              print_buffer);
    201   } else if (0 == memcmp(pString, "RECV", 0x04)) {
    202     ALOGD_IF(ese_debug_enabled, "NxpEseDataR len = %3d > %s", len,
    203              print_buffer);
    204   }
    205 
    206   return;
    207 }
    208 
    209 /*******************************************************************************
    210 **
    211 ** Function         phPalEse_sleep
    212 **
    213 ** Description      This function  suspends execution of the calling thread for
    214 **                  (at least) usec microseconds
    215 **
    216 ** Returns          None
    217 **
    218 *******************************************************************************/
    219 void phPalEse_sleep(long usec) {
    220   usleep(usec);
    221   return;
    222 }
    223 
    224 /**
    225  * \ingroup eSe_PAL
    226  * \brief This function updates destination buffer with val
    227  *                 data in len size
    228  *
    229  * \param[in]    buff                - Array to be udpated
    230  * \param[in]    val                 - value to be updated
    231  * \param[in]    len                 - length of array to be updated
    232  *
    233  * \retval   void
    234  *
    235  */
    236 void* phPalEse_memset(void* buff, int val, size_t len) {
    237   return memset(buff, val, len);
    238 }
    239 
    240 /**
    241  * \ingroup eSe_PAL
    242  * \brief This function copies source buffer to  destination buffer
    243  *                 data in len size
    244  *
    245  * \param[in]    dest                - Destination array to be updated
    246  * \param[in]    src                 - Source array to be updated
    247  * \param[in]    len                 - length of array to be updated
    248  *
    249  * \retval   void
    250  *
    251  */
    252 void* phPalEse_memcpy(void* dest, const void* src, size_t len) {
    253   return memcpy(dest, src, len);
    254 }
    255 
    256 /**
    257  * \ingroup eSe_PAL
    258  * \brief This is utility function for runtime heap memory allocation
    259  *
    260  * \param[in]    size                 - number of bytes to be allocated
    261  *
    262  * \retval   void
    263  *
    264  */
    265 void* phPalEse_memalloc(uint32_t size) { return malloc(size); }
    266 
    267 /**
    268  * \ingroup eSe_PAL
    269  * \brief This is utility function for runtime heap memory allocation
    270  *
    271  * \param[in]    len                 - number of bytes to be allocated
    272  *
    273  * \retval   void
    274  *
    275  */
    276 void* phPalEse_calloc(size_t datatype, size_t size) {
    277   return calloc(datatype, size);
    278 }
    279 
    280 /**
    281  * \ingroup eSe_PAL
    282  * \brief This is utility function for freeeing heap memory allocated
    283  *
    284  * \param[in]    ptr                 - Address pointer to previous allocation
    285  *
    286  * \retval   void
    287  *
    288  */
    289 void phPalEse_free(void* ptr) { return free(ptr); }
    290